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

你可能感兴趣的文章
Netty工作笔记0069---Protobuf使用案例
查看>>
Netty工作笔记0070---Protobuf使用案例Codec使用
查看>>
Netty工作笔记0071---Protobuf传输多种类型
查看>>
Netty工作笔记0072---Protobuf内容小结
查看>>
Netty工作笔记0073---Neety的出站和入站机制
查看>>
Netty工作笔记0074---handler链调用机制实例1
查看>>
Netty工作笔记0075---handler链调用机制实例1
查看>>
Netty工作笔记0076---handler链调用机制实例3
查看>>
Netty工作笔记0077---handler链调用机制实例4
查看>>
Netty工作笔记0078---Netty其他常用编解码器
查看>>
Netty工作笔记0079---Log4j整合到Netty
查看>>
Netty工作笔记0080---编解码器和处理器链梳理
查看>>
Netty工作笔记0081---编解码器和处理器链梳理
查看>>
Netty工作笔记0082---TCP粘包拆包实例演示
查看>>
Netty工作笔记0083---通过自定义协议解决粘包拆包问题1
查看>>
Netty工作笔记0084---通过自定义协议解决粘包拆包问题2
查看>>
Netty工作笔记0085---TCP粘包拆包内容梳理
查看>>
Netty常用组件一
查看>>
Netty常见组件二
查看>>
Netty应用实例
查看>>