wpf-基础-命令-自定义命令

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

实例:自定义控件和命令

点击清除,清空绿框内三个TextBox里的内容。
在这里插入图片描述
控件UserControl1.xaml

<UserControl x:Class="pxy.UserControl1"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:pxy"
             mc:Ignorable="d" 
             d:DesignHeight="114" d:DesignWidth="200">
    <Border CornerRadius="5" BorderBrush="LawnGreen" BorderThickness="2">
        <StackPanel>
            <TextBox x:Name="textBox1" Margin="5"/>
            <TextBox x:Name="textBox2" Margin="5,0"/>
            <TextBox x:Name="textBox3" Margin="5"/>
            <TextBox x:Name="textBox4" Margin="5,0"/>
        </StackPanel>
    </Border>
</UserControl>

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

namespace pxy
{
    /// <summary>
    /// UserControl1.xaml 的交互逻辑
    /// </summary>
    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();
        }
    }
}

主窗口前端:前面省略了

    <StackPanel>
        <local:MyCommandSource x:Name="ctrlClear" Margin="10">
            <TextBlock Text="清除" FontSize="16" TextAlignment="Center" Width="80" Background="Orchid"/>
        </local:MyCommandSource>
        <local:UserControl1 x:Name="UserControl1"/>
    </StackPanel>

主窗口后台
其实是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);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值