博客
关于我
wpf-基础-命令-自定义命令
阅读量:660 次
发布时间:2019-03-15

本文共 2707 字,大约阅读时间需要 9 分钟。

RoutedCommand与业务逻辑无关,业务逻辑要依靠外围的CommandBinding来实现。

实例:自定义控件和命令

点击清除,清空绿框内三个TextBox里的内容。

在这里插入图片描述
控件UserControl1.xaml

控件的后台:继承用来自定义命令的接口

namespace pxy{    ///     /// UserControl1.xaml 的交互逻辑    ///     public partial class UserControl1 : UserControl, IView    {        public UserControl1()        {            InitializeComponent();        }                public bool IsChanged { get; set; }                public void Clear()        {            this.textBox1.Clear();            this.textBox2.Clear();            this.textBox3.Clear();            this.textBox4.Clear();        }                //以下是继承IView后自动生成的        public void Refresh()        {            throw new NotImplementedException();        }                public void Save()        {            throw new NotImplementedException();        }                public void SetBinding()        {            throw new NotImplementedException();        }    }}

主窗口前端:前面省略了

主窗口后台

其实是ICommand的子类实现逻辑的。MyCommandSource则指定了何时触发命令。另外,主窗口的构造函数中,放置了自定义命令的声明。其实应该将命令声明放在静态全局处供所有对象使用。

public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();            ClearCommand clearCommand = new ClearCommand();            this.ctrlClear.Command = clearCommand;            this.ctrlClear.CommandTarget = this.UserControl1;        }    }        public interface IView {         bool IsChanged { get; set; }        void SetBinding();        void Refresh();        void Clear();        void Save();    }        // 自定义命令    public class ClearCommand : ICommand    {// 继承ICommand需要有以下的属性和命令(vs自动补全)        public event EventHandler CanExecuteChanged;        public bool CanExecute(object parameter)        {//判断命令是否可以执行            throw new NotImplementedException();        }        //命令执行,带有与业务相关的Clear逻辑        public void Execute(object parameter)        {            IView view = parameter as IView;            if (view != null)                view.Clear();        }    }        // 自定义命令源    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)        {// 重写这个是为了让控件被左单击时执行命令。如果是Button可能要捕捉Button.Click。            base.OnMouseLeftButtonDown(e);            if (this.CommandTarget != null)                this.Command.Execute(this.CommandTarget);        }    }}

转载地址:http://smelz.baihongyu.com/

你可能感兴趣的文章
MySQL锁,锁的到底是什么?
查看>>
MySQL错误-this is incompatible with sql_mode=only_full_group_by完美解决方案
查看>>
Mysql错误2003 -Can't connect toMySQL server on 'localhost'(10061)解决办法
查看>>
MySQL错误提示mysql Statement violates GTID consistency
查看>>
mysql错误:This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its de
查看>>
mysql长事务
查看>>
mysql问题记录
查看>>
MySQL集群解决方案(1):MySQL数据库的集群方案
查看>>
MySQL集群解决方案(4):负载均衡
查看>>
MySQL集群解决方案(5):PXC集群
查看>>
MySQL面试宝典
查看>>
WAP短信:融合传统短信和互联网的新型通信方式
查看>>
mysql面试题学校三表查询_mysql三表查询分组后取每组最大值,mysql面试题。
查看>>
Mysql面试题精选
查看>>
MySQL面试题集锦
查看>>
mysql面试题,存储引擎InnoDB和MyISAM
查看>>
mysql面试题:为什么MySQL单表不能超过2000W条数据?
查看>>
mysql面试题:创建索引时会不会锁表?
查看>>
mysql面试题:高度为3的B+树可以存放多少数据?
查看>>
mysql颠覆实战笔记(八)--mysql的自定义异常处理怎么破
查看>>