欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-03-04 11:29:50
...

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

实例:自定义控件和命令

点击清除,清空绿框内三个TextBox里的内容。
wpf-基础-命令-自定义命令
控件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);
        }
    }
}
相关标签: c#和WPF wpf