7
我想打开基于某些自定义逻辑的帮助文件到页面。我如何处理用户在我的所有窗口(主窗口和模式对话框)上按F1?如何在每个WPF窗口中处理快捷键?
我知道如何在单个窗口中处理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();
}
}
}
它也适用于子控件。即如果选择了子控件,那么它将处理F1,如果它的类型注册处理程序,否则窗口处理程序被触发。 – 2012-07-05 10:15:13