显然,只有在这样的代码中定义命令时,这才是可能的。
public class MyCommands
{
public static RoutedUICommand NextPage { get; private set; }
public static RoutedUICommand PreviousPage { get; private set; }
static OCRopingCommands()
{
NextPage = new RoutedUICommand("NextPage", "NextPage", typeof(MyCommands));
NextPage.InputGestures.Add(
new KeyGesture(Key.PageDown, ModifierKeys.None, "PageDn"));
PreviousPage = new RoutedUICommand("PreviousPage", "PreviousPage", typeof(MyCommands));
PreviousPage.InputGestures.Add(
new KeyGesture(Key.PageUp, ModifierKeys.None, "PageUp"));
}
}
这是你给他们接线的方法
<MenuItem Header="_Previous Page"
Command="local:MyCommands.PreviousPage">
<MenuItem.Icon>
<Image Source="Images\Backward.png"
Stretch="Uniform"/>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="_Next Page"
Command="local:MyCommands.NextPage">
<MenuItem.Icon>
<Image Source="Images\Forward.png"
Stretch="Uniform"/>
</MenuItem.Icon>
</MenuItem>
为什么
KeyGesture
不允许您在XAML中设置显示名称超出我的范围。