博客
关于我
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/

你可能感兴趣的文章
ngrok内网穿透可以实现资源共享吗?快解析更加简洁
查看>>
NHibernate动态添加表
查看>>
NHibernate学习[1]
查看>>
NHibernate异常:No persister for的解决办法
查看>>
Nhibernate的第一个实例
查看>>
NHibernate示例
查看>>
nid修改oracle11gR2数据库名
查看>>
NIFI1.21.0/NIFI1.22.0/NIFI1.24.0/NIFI1.26.0_2024-06-11最新版本安装_采用HTTP方式_搭建集群_实际操作---大数据之Nifi工作笔记0050
查看>>
NIFI1.21.0_java.net.SocketException:_Too many open files 打开的文件太多_实际操作---大数据之Nifi工作笔记0051
查看>>
NIFI1.21.0_Mysql到Mysql增量CDC同步中_日期类型_以及null数据同步处理补充---大数据之Nifi工作笔记0057
查看>>
NIFI1.21.0_Mysql到Mysql增量CDC同步中_补充_插入时如果目标表中已存在该数据则自动改为更新数据_Postgresql_Hbase也适用---大数据之Nifi工作笔记0058
查看>>
NIFI1.21.0_Mysql到Mysql增量CDC同步中_补充_更新时如果目标表中不存在记录就改为插入数据_Postgresql_Hbase也适用---大数据之Nifi工作笔记0059
查看>>
NIFI1.21.0_NIFI和hadoop蹦了_200G集群磁盘又满了_Jps看不到进程了_Unable to write in /tmp. Aborting----大数据之Nifi工作笔记0052
查看>>
NIFI1.21.0_Postgresql和Mysql同时指定库_指定多表_全量同步到Mysql数据库以及Hbase数据库中---大数据之Nifi工作笔记0060
查看>>
NIFI1.21.0最新版本安装_连接phoenix_单机版_Https登录_什么都没改换了最新版本的NIFI可以连接了_气人_实现插入数据到Hbase_实际操作---大数据之Nifi工作笔记0050
查看>>
NIFI1.21.0最新版本安装_配置使用HTTP登录_默认是用HTTPS登录的_Https登录需要输入用户名密码_HTTP不需要---大数据之Nifi工作笔记0051
查看>>
NIFI1.21.0通过Postgresql11的CDC逻辑复制槽实现_指定表多表增量同步_增删改数据分发及删除数据实时同步_通过分页解决变更记录过大问题_02----大数据之Nifi工作笔记0054
查看>>
NIFI1.21.0通过Postgresql11的CDC逻辑复制槽实现_指定表多表增量同步_增加修改实时同步_使用JsonPath及自定义Python脚本_03---大数据之Nifi工作笔记0055
查看>>
NIFI1.21.0通过Postgresql11的CDC逻辑复制槽实现_指定表多表增量同步_插入修改删除增量数据实时同步_通过分页解决变更记录过大问题_01----大数据之Nifi工作笔记0053
查看>>
NIFI1.21.0通过Postgresql11的CDC逻辑复制槽实现_指定表或全表增量同步_实现指定整库同步_或指定数据表同步配置_04---大数据之Nifi工作笔记0056
查看>>