按键(ESC ,F1,F2等)——wpf的命令处理方法

WPF窗体的命令绑定

 

方法一:使用代码

  <WpfUI:View.CommandBindings>
        <CommandBinding Command="Help"
                        CanExecute="HelpCanExecute"
                        Executed="HelpExecuted"
                        />
    </WpfUI:View.CommandBindings>
复制代码
<WpfUI:View.InputBindings>
        <KeyBinding Command="Help" Key="F2" />
        <KeyBinding Command="NotACommand" Key="F1"/>
    </WpfUI:View.InputBindings>
 #region Command

        private void HelpCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private void HelpExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            System.Diagnostics.Process.Start("http://www.baidu.com");
        }

        #endregion Command
复制代码

构造函数

复制代码
  public TallyBookView()
        {
            InitializeComponent();

            this.InputBindings.Add(
                new KeyBinding(ApplicationCommands.Help, new KeyGesture(Key.F2)));
        }
复制代码

上面等同于如下代码:

posted @ 2015-06-18 10:53  长白山  阅读(881)  评论(0编辑  收藏  举报