我正在编写我的第一个
WPF应用程序,并试图让我的自定义命令工作.
public static RoutedUICommand Header1 { get; private set; } . . . gestures = new InputGestureCollection(); gestures.Add(new KeyGesture(Key.D1,ModifierKeys.Control,"Ctrl+1")); Header1 = new RoutedUICommand("Header 1","Header1",typeof(EditCommands),gestures);
然后我在我的窗口的XAML中添加了一个CommandBindings部分.
<!-- local refers to my application's namespace --> <Window.CommandBindings> <CommandBinding Command="local:EditCommands.Header1" Executed="CommandBinding_Executed" CanExecute="CommandBinding_CanExecute"></CommandBinding> </Window.CommandBindings>
最后,在关联的Ribbon控件中添加了一个命令条目.
<RibbonButton Label="Header 1" Command="local:EditCommands.Header1" SmallImageSource="Images\small.png" ToolTipTitle="Header 1" ToolTipDescription="" ToolTipImageSource="Images\small.png"></RibbonButton>
单击功能区按钮可按预期执行处理程序.但是,按Ctrl 1似乎完全没有效果.如何识别我的热键?
解决方法
还有其他事情必须继续.是否有一些元素在使用包含命令的命令绑定到达元素之前处理键输入?
Snooop是一个很有用的工具来搞清楚这样的事情.
每按一次Ctrl 1,就会调用HandleHeader1下面的代码.
public static class MyCommands { public static RoutedUICommand Header1 { get; } = new RoutedUICommand("Header 1",typeof(MyCommands),new InputGestureCollection { new KeyGesture(Key.D1,"Ctrl+1") }); } public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } private void HandleHeader1(object sender,ExecutedRoutedEventArgs e) { } } <Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApplication2" mc:Ignorable="d" Title="MainWindow" Height="350" Width="525"> <Window.CommandBindings> <CommandBinding Command="local:MyCommands.Header1" Executed="HandleHeader1"/> </Window.CommandBindings> <StackPanel> <TextBox/> </StackPanel> </Window>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。