zoukankan      html  css  js  c++  java
  • WPF自定义命令

    WPF的自定义命令实现过程包括三个部分,定义命令、定义命令源、命令调用,代码实现如下:

        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
                
            }
    
            /// <summary>
            /// 自定义命令演示
            /// </summary>
            public void TestUserDefineCommand() {
                UserDefineCommandSource source = new UserDefineCommandSource();
                UserDefineCommand userCmd = new UserDefineCommand();
                source.Command = userCmd;
                source.CommandTarget = this;
            }
        }
    
        public class UserDefineCommand : ICommand
        {
            public event EventHandler CanExecuteChanged;
            public bool CanExecute(object parameter)
            {
                throw new NotImplementedException();
            }
            public void Execute(object parameter)
            {
                
            }
        }
    
        public class UserDefineCommandSource : UserControl, ICommandSource
        {
            public ICommand Command { get; set; }
            public object CommandParameter { get; set; }
            public System.Windows.IInputElement CommandTarget { get; set; }
    
            protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
            {
                base.OnMouseLeftButtonDown(e);  
                if (this.CommandTarget != null && this.Command != null)
                    this.Command.Execute(this.CommandTarget);
            }
        }
    

      Button控件的定义如下:public abstract class ButtonBase : ContentControl, ICommandSource,因为实现了ICommandSource接口,因此,可以Button为命令源,可以为其设置命令。

  • 相关阅读:
    安全事件关联分析方法
    网络安全公开数据集
    2019年汽车网络安全
    基于知识图谱的APT组织追踪治理——实践性比较强
    Hutool中常用的工具类和方法
    阿里云短信服务
    Java8实现List转Map
    centos下安装nodejs
    微信小程序,联系客服
    mysql空闲连接
  • 原文地址:https://www.cnblogs.com/zhaiyf/p/8430897.html
Copyright © 2011-2022 走看看