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

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

WPF 应用程序中的命令与自定义控件实现

在 WPF 应用程序开发中,命令(Command)是实现应用程序逻辑的重要机制。通过命令,可以将 UI 元素与业务逻辑分开,使 UI 界面更加清晰可维护。然而,RoutedCommand 与业务逻辑无关,业务逻辑应依靠外围的 CommandBinding 来实现。

自定义控件与命令

1.1 自定义控件的实现

以下是一个简单的自定义控件示例,用于清除三个 TextBox 中的内容。

1.2 控件的后台逻辑

namespace pxy{    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();        }        public void Refresh()        {            throw new NotImplementedException();        }        public void Save()        {            throw new NotImplementedException();        }        public void SetBinding()        {            throw new NotImplementedException();        }    }}

1.3 主窗口逻辑

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{    public event System.EventHandler CanExecuteChanged;    public bool CanExecute(object parameter)    {        throw new NotImplementedException();    }    public void Execute(object parameter)    {        IView view = parameter as IView;        if (view != null)            view.Clear();    }}public class MyCommandSource : UserControl, 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 (this.CommandTarget != null)            this.Command.Execute(this.CommandTarget);    }}

命令与业务逻辑的结合

在 WPF 应用程序中,命令是用来实现业务逻辑的核心机制。通过 CommandBinding 将命令与 UI 元素绑定,可以实现 UI 元素与业务逻辑的分离。自定义命令的实现需要继承 ICommand 接口,并实现 CanExecuteExecute 方法。

在本文中,ClearCommand 是一个自定义命令,用于清除三个 TextBox 中的内容。MyCommandSource 是一个自定义的命令源控件,它能够捕捉左键单击事件并执行命令。通过将 ClearCommand 绑定到 MyCommandSource,可以实现清除功能。

总结

通过以上实现,可以清晰地看到 WPF 应用程序中命令与自定义控件的结合方式。命令负责实现业务逻辑,自定义控件负责定义 UI 界面,两者通过 CommandBinding 实现了紧密的耦合。这种设计使得 UI 界面更加清晰,同时也提高了代码的可维护性和扩展性。

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

你可能感兴趣的文章
Objective-C实现判断位是不是偶数isEven算法(附完整源码)
查看>>
Objective-C实现判断字符串是否包含特殊字符算法(附完整源码)
查看>>
Objective-C实现判断字符串是否回文palindrome算法(附完整源码)
查看>>
Objective-C实现判断数是否为质数(附完整源码)
查看>>
Objective-C实现判断整数是否为2的幂isPowerOfTwo算法(附完整源码)
查看>>
Objective-C实现判断是否为回文字符串(附完整源码)
查看>>
Objective-C实现判断是否为回文数算法(附完整源码)
查看>>
Objective-C实现判断正整数n的d进制数表示形式是否是回文数(附完整源码)
查看>>
Objective-C实现判断闰年(附完整源码)
查看>>
Objective-C实现利用stack对输入的式子进行计算算法(附完整源码)
查看>>
Objective-C实现前缀Knuth–Morris–Pratt 算法(附完整源码)
查看>>
Objective-C实现加密哈希SHA-1 算法(附完整源码)
查看>>
Objective-C实现动态规划之棒材切割算法(附完整源码)
查看>>
Objective-C实现勒让德多项式(附完整源码)
查看>>
Objective-C实现区域生长法(附完整源码)
查看>>
Objective-C实现十六进制转二进制算法(附完整源码)
查看>>
Objective-C实现十六进制转十进制算法(附完整源码)
查看>>
Objective-C实现十进制转N进制算法(附完整源码)
查看>>
Objective-C实现十进制转二进制(附完整源码)
查看>>
Objective-C实现十进制转八进制算法(附完整源码)
查看>>