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为命令源,可以为其设置命令。

  • 相关阅读:
    Linux常用快捷键
    如何Oracle 数据库备份与恢复
    Linux常用命令解释
    转摘:商业智能BI的演绎型需求和归纳型需求BI三维框架之内容维研究
    PHP中const的使用
    PHP中define的使用
    Apache配置正向代理与反向代理
    正向代理
    JAVA System.getProperty()参数
    PHP查找当前URL文件扩展名
  • 原文地址:https://www.cnblogs.com/zhaiyf/p/8430897.html
Copyright © 2011-2022 走看看