WPF之Command基础

 时间:2026-04-22 10:19:08

1、界面布局,文本框和按钮

WPF之Command基础

WPF之Command基础

2、声明并定义命令

private RoutedCommand sayCmd = new RoutedCommand("Say", typeof(MainWindow));

WPF之Command基础

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);

WPF之Command基础

8、命令可执行逻辑

e.CanExecute = myText.Text.ToString() == "" ? false : true;

WPF之Command基础

9、执行命令

MessageBox.Show(myText.Text);

WPF之Command基础

10、执行效果如下

WPF之Command基础

WPF之Command基础

  • IBM system X3250 M4 配置RAID阵列
  • .net密码写入与读出加密狗
  • 如何通过注册表查找远程端口号
  • h3c 华三路由器之查看管理ip地址
  • 批处理命令教程
  • 热门搜索
    脖子粗是什么原因 k宝是什么 高达是什么 什么是原发性高血压 9月18日是什么日子 审车需要带什么资料 浮萍是什么意思 车架号是什么 cpu是什么意思 become是什么意思