WPF中Cammand命令
来源:互联网 发布:asp嵌入java代码 编辑:程序博客网 时间:2024/06/16 09:35
Cammand命令
命令
实现了ICammand接口,常用的是继承RoutedCommand子类
命令源
实现了ICommandSource接口
命令目标
实现了IInpurElement接口,常常是直接对UI控件进行扩展
命令多使用单件模式………Singletone Pattern
1.创建命令类
2.声明命令实例
3.指定命令源
4.指定命令目标
5.设置命令关联
CommandManager:
PreviewCanExecute , PreviewExecute
CanExecute,Executed
CommandSource有Command,
CommandSource不断的向CommandTarget发送CanExecute和Executed,
CommandTarget向CommandBinding发送路由事件,
CommandBinding捕捉并处理事件,向Command反馈。
//定义命令private RoutedCommand clearCmd = new RoutedCommand("Clear",typeof(Window1));...private void InitializeCommand(){ //指定命令源---------给控件添加命令 this.button1.Command = this.clearCmd; this.clearCmd = InputGesture.Add(new KeyGesture(Key.C,Modifierkeys.Alt)); //指定命令目标 this.button1.CommandTarget = this.textBoxA; 创建CommandBinding CommandBinding cb = new CommandBinding(); cb.Command = this.clearCmd; cb.CanExecute += new CanExecuteRoutedEventHandler(cb_CanExecute); cb.Executed += new ExecutedRoutedEventHandler(cb_Executed); //把CommandBinding设置在外围控件上 this.stackPanel.CommandBindings.Add(cb);} void cb_CanExecute(object sender , CanExecuteRoutedEventArgs e){ if(string IsNullOrEmpty(this.textBoxA.Text)){ e.CanExecute = false; }else{ e.CanExecute = true} e.Handled = true; } void cb_Executed(object sender , CanExecuteRoutedEventArgs e){ this.textBoxA.Clear(); e.Handled = true; }
命令参数 CommandParamter—命令携带的信息
<Button ... Command = "New" CommandParamter = "Teacher"/><Button ... Command = "New" CommandParamter = "Student"/>...<Window.CommandBinds> <CommandBinding Command = "New" CommandParamter = "Teacher" CanExecute = "New_CanExecute" Executed = "New_Executed"/></Window.CommandBinds>...private void New_Executed(object sender , CanExecuteRoutedEventArgs e){ if(e.Parameter.ToString() == "Teacher"){ ... } if(e.Parameter.ToString() == "Student"){ ... }}
命令与Binding结合…使命令的赋值更加灵活
<Button x:Name = "dynamicCmdBtn" Command = "{Binding Path=ppp,Source=sss}" Content = "Command" }
可以根据特定条件改变命令
ICommand接口:
1.CanExecute方法
2.Execute方法
3.CanExecuteChanged事件
ICommandSource接口
1.Command属性
2.CommandParamter属性
3.CommandTarget属性
RoutedCommand类…实现了Icommand接口的类
并未向CanExecute方法和Execute方法添加任何逻辑
RoutedCommand与业务逻辑无关
业务逻辑要依靠外围组件的CommandBinding来实现
尽量把逻辑卸载Execute方法内
自定义命令
1.声明定义自己的RoutedCommand实例
2.实现ICommand接口,实现自己的业务逻辑,实现ICommandSource接口
例:
//定义接口public interface IView{ bool IsChanged {get;set;} void SetBinding(); void Refresh(); void Clear(); void Save();}//实现ICommand接口,创建一个作用于IView的命令public class ClearCommand : ICommand{ public event EventHandler CanExecuteChanged; public bool CanExecute(object Parameter){...}; public void Execute(object Parameter){ IView view = Paramter as IView; if( view != null ){ view.Clear(); } }}//实现ICommandSource接口 public class MyCommandSource : UserControl,ICommandSource{ //继承自 ICommandSource的三个属性 public ICommand Command{get;set;} public object CommandParameter{get;set;} public IInputElement CommandTarget{get;set;} //实现点击事件 protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e){ base.OnMouseLeftButtonDown(e); if(e.CommandTarget != null){ this.Command.Execute(this.CommandTarget); } } }
实现IView的子类时先编辑XAML,再实现IView接口
InitializeComponent();...ClearCommand clearCmd = new ClearCommand();this.ctrlClear.Command = clearCmd;this.ctrlClear.CommandTarget = this.mineView//minewView实现IView接口
- WPF中Cammand命令
- WPF中命令的绑定
- WPF 在事件中绑定命令
- WPF:命令
- wpf命令
- (WPF)命令
- WPF:命令
- wpf 命令
- WPF中的命令(二)- 命令中传递参数
- wpf笔记--wpf命令库
- WPF中的命令简述
- WPF学习一:命令
- WPF中的命令简述
- WPF(命令)
- WPF(命令参数)
- WPF(命令)
- WPF之命令
- WPF中的命令
- BZOJ 2005-能量采集(莫比乌斯反演)
- C# Messager深入
- spark-schedule
- Thinkphp3.2使用问题
- Sass入门二
- WPF中Cammand命令
- Mysql插入大量数据
- Bootstrap的栅格系统
- Mysql中MyISAM引擎和InnoDB引擎的比较
- fast-DTW算法 python实现
- webview的使用套餐
- 压力测试工具siege的用法
- 数据库的连接、索引和Redis的五种数据类型及其操作命令、使用场景
- JEESZ分布式框架--开发环境部署