3

メニュー項目にバインドされているWPFコマンドにキーボードショートカットをバインドしようとしている非常に単純なアプリケーションがあります。アプリケーション自体は、MenuWebBrowserコントロールだけで構成されています。

内にいるときWebBrowser、キーボードショートカットはWPFメニューにルーティングされません。たとえば、Webブラウザでフォーカスされているときに「Ctrl + O」と入力すると、IEの開いているページが表示されます。さらに、このアプリケーションでは、(Altを入力して)メニューにフォーカスを設定しない限り、入力バインディングは起動しません。たとえば、タイトルバーをクリックしてからショートカットを入力しても、WPFウィンドウに焦点を合わせることができません。完全なコードは以下に複製されます:

MainWindow.xaml

<Window x:Class="TestInputBindingsOnMenu.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="600" Width="800">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Menu IsMainMenu="True" x:Name="_mainMenu" Grid.Row="0" />
        <WebBrowser Source="http://google.com" Grid.Row="1" />
    </Grid>
</Window>

MainWindow.xaml.cs

using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;

namespace TestInputBindingsOnMenu
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            Initialize();
        }

        private void Initialize()
        {
            MenuItem fileMenu = new MenuItem();
            MenuItem fileNew = new MenuItem();
            MenuItem fileOpen = new MenuItem();
            MenuItem fileExit = new MenuItem();

            fileMenu.Header = "File";
            fileNew.Header = "New";
            fileOpen.Header = "Open";
            fileExit.Header = "Exit";

            fileMenu.Items.Add(fileNew);
            fileMenu.Items.Add(fileOpen);
            fileMenu.Items.Add(fileExit);

            _mainMenu.Items.Add(fileMenu);

            var fileNewCommand = CreateCommand("New");
            var fileOpenCommand = CreateCommand("Open");
            var fileExitCommand = CreateCommand("Exit");

            _mainMenu.CommandBindings.Add(new CommandBinding(fileNewCommand, ExecuteNew));
            _mainMenu.CommandBindings.Add(new CommandBinding(fileOpenCommand, ExecuteOpen));
            _mainMenu.CommandBindings.Add(new CommandBinding(fileExitCommand, ExecuteExit));

            fileNew.Command = fileNewCommand;
            fileOpen.Command = fileOpenCommand;
            fileExit.Command = fileExitCommand;

            _mainMenu.InputBindings.Add(new InputBinding(fileNewCommand, new KeyGesture(Key.N, ModifierKeys.Control)));
            _mainMenu.InputBindings.Add(new InputBinding(fileOpenCommand, new KeyGesture(Key.O, ModifierKeys.Control)));
            _mainMenu.InputBindings.Add(new InputBinding(fileExitCommand, new KeyGesture(Key.F4, ModifierKeys.Alt)));
        }

        private void ExecuteNew(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("New!!");
        }

        private void ExecuteOpen(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Open!!");
        }

        private void ExecuteExit(object sender, ExecutedRoutedEventArgs e)
        {
            MessageBox.Show("Exit!!");
        }

        private static RoutedCommand CreateCommand(string label)
        {
            return new RoutedCommand(label, typeof(MainWindow));
        }
    }
}
4

1 に答える 1

2

簡単な解決策

入力バインディングをWebBrowserコントロールとメインメニューに追加します。

Browser.InputBindings.Add(new KeyBinding(ApplicationCommands.Open, 
new KeyGesture(Key.O, ModifierKeys.Control)));

ハードソリューション

ここで起こっていることは、イベントをUIElement使用しているときにイベントを使用していること、またはルーティングされたイベントも処理するハンドラーを追加していることです。これについて知らない場合は、トンネリングとバブリングを調べてください。KeyDownPreviewKeyDownHandled

これはUIElementクラスで処理されるため、この状況では別のパターンを使用することをお勧めします。MVVMLightフレームワークは動作を提供しEventToCommandます。コレクションで使用するPreviewKeyDown代わりに、ウィンドウのイベントを適切なコマンドにルーティングできる場合は、ソリューションが得られます。KeyBindingInputBindingsUIElement

どのキーが押されたか、どのコマンドにルーティングするかを確認するには、カスタムコードが必要になります。

于 2011-11-28T15:48:47.347 に答える