WPF之Command基础
1、界面布局,文本框和按钮


2、声明并定义命令private RoutedCommand sayCmd = new RoutedCommand("Say", typeof(MainWindow));

3、指定命令的源(命令的发送者)myBtn.Command = sayCmd;
4、指定快捷键sayCmd.InputGestures.Add(new KeyGesture(Key.S, ModifierKeys.Alt));
5、指定命令目标myBtn.CommandTarget = myText;
6、创建命令关联CommandBinding cb = new CommandBinding();cb.Command = sayCmd;cb.CanExecute += new CanExecuteRoutedEventHandler(cb_CanExecute);cb.Executed += new ExecutedRoutedEventHandler(cb_Executed);
7、把命令关联安置在外围控件上grd.CommandBindings.Add(cb);

8、命令可执行逻辑e.CanExecute = myText.Text.ToString() == "" ? false : true;

9、执行命令MessageBox.Show(myText.Text);

10、执行效果如下

