博客
关于我
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通用优化手册
查看>>
Mysql通过data文件恢复
查看>>
MYSQL遇到Deadlock found when trying to get lock,解决方案
查看>>
MYSQL遇到Deadlock found when trying to get lock,解决方案
查看>>
mysql部署错误
查看>>
MySQL配置信息解读(my.cnf)
查看>>
Mysql配置文件my.ini详解
查看>>
MySQL配置文件深度解析:10个关键参数及优化技巧---强烈要求的福利来咯。
查看>>
Mysql配置表名忽略大小写(SpringBoot连接表时提示不存在,实际是存在的)
查看>>
mysql配置读写分离并在若依框架使用读写分离
查看>>
MySQL里为什么会建议不要使用SELECT *?
查看>>
MySQL里的那些日志们
查看>>
MySQL锁
查看>>
MySQL锁与脏读、不可重复读、幻读详解
查看>>
MySQL锁机制
查看>>
mysql锁机制,主从复制
查看>>
Mysql锁机制,行锁表锁
查看>>
MySQL锁表问题排查
查看>>
Mysql锁(1):锁概述和全局锁的介绍
查看>>