いくつかのカスタム ロジックに基づいてページのヘルプ ファイルを開きたいと考えています。すべてのウィンドウ (メイン ウィンドウとモーダル ダイアログ) でユーザーが F1 キーを押した場合、どのように処理すればよいですか?
単一のウィンドウで F1 を処理する方法は知っていますが、これをグローバルに実行できるので、すべてのウィンドウに同じコードを追加する必要はありません。
以下は、子ウィンドウで F1 が機能しないことを試したテストです。
Window1.xaml:
<Window x:Class="WpfApplication2.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Window.CommandBindings>
<CommandBinding Command="ApplicationCommands.Help"
Executed="CommandBinding_Executed"/>
</Window.CommandBindings>
<Grid>
<Button Content="Open a new window"
Click="Button_Click"/>
</Grid>
</Window>
Window1.xaml.cs:
using System.Windows;
using System.Windows.Input;
namespace WpfApplication2
{
partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
}
void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
MessageBox.Show("Help");
}
void Button_Click(object sender, RoutedEventArgs e)
{
new Window().ShowDialog();
}
}
}