RoutedCommand简单用法

来源:互联网 发布:linux red5安装与配置 编辑:程序博客网 时间:2024/06/14 18:03

Command都是继承自ICommand接口,wpf内部实现了RoutedCommand类,内部还有常用的静态RoutedUICommand实例

    ApplicationCommands

    ComponentCommands

    NavigationCommands

    MediaCommands

    EditingCommands

常见的操作都应该够用,如果不够用就自己加,如果要实现自己的业务逻辑,也需要自己去实现ICommand

1.创建命令

    RoutedCommand command = new RoutedCommand("Show", typeof(Window));

   //RoutedCommand command = ApplicationCommands.New;

   command.InputGestures.Add(new KeyGesture(Key.C, ModifierKeys.Alt);//组合键

2.绑定命令

   this.btn.Command = command;

   CommandBinding cb = new CommandBinding(command, Cb_Executed, Cb_CanExecute);

   this.stackPanel.CommandBinding.Add(cb);

3.Exe,CanExe方法

   private void Cb_Executed(object sender, ExecutedRoutedEventArgs e)

   { 

        ...

   }

   private void Cb_CanExecute(object sender, CanExecuteRoutedEventArgs e)

   {

        if ()

             e.CanExecute = true;

       else

            e.CanExecute = false;

   }


xaml写法

<Window.CommandBinding>

    <CommandBinding Command="New" CanExecute="Cb_CanExecute" Execute="Cb_Execute"/>

</Window.CommandBinding>


<Button Command="New">

原创粉丝点击