zoukankan      html  css  js  c++  java
  • WPF customize DelegateCommand

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows;
    
    namespace WpfApp55.ViewModel
    {
        public class VM : INotifyPropertyChanged
        {
            public event PropertyChangedEventHandler PropertyChanged;
    
            public void OnPropertyChanged(string propName)
            {
                var handler = PropertyChanged;
                if (handler != null)
                {
                    handler(this, new PropertyChangedEventArgs(propName));
                }
            }
    
            private DelegateCommand UCCmdValue;
            public DelegateCommand UCCmd
            {
                get
                {
                    if(UCCmdValue==null)
                    {
                        UCCmdValue = new DelegateCommand(UCCmdExecuted, UCCmdCanExecute);
                    }
                    return UCCmdValue;
                }
            }
    
            private bool UCCmdCanExecute(object obj)
            {
                return true;
            }
    
            private void UCCmdExecuted(object obj)
            {
                MessageBox.Show("You had clicked the customized button!");
            }
        }
    }
     1  public class DelegateCommand : ICommand
     2     {
     3         private readonly Predicate<object> _canExecute;
     4         private readonly Action<object> _execute;
     5 
     6         public event EventHandler CanExecuteChanged;
     7 
     8         public DelegateCommand(Action<object> execute)
     9                        : this(execute, null)
    10         {
    11         }
    12 
    13         public DelegateCommand(Action<object> execute,
    14                        Predicate<object> canExecute)
    15         {
    16             _execute = execute;
    17             _canExecute = canExecute;
    18         }
    19 
    20         public  bool CanExecute(object parameter)
    21         {
    22             if (_canExecute == null)
    23             {
    24                 return true;
    25             }
    26 
    27             return _canExecute(parameter);
    28         }
    29 
    30         public  void Execute(object parameter)
    31         {
    32             _execute(parameter);
    33         }
    34 
    35         public void RaiseCanExecuteChanged()
    36         {
    37             if (CanExecuteChanged != null)
    38             {
    39                 CanExecuteChanged(this, EventArgs.Empty);
    40             }
    41         }
    42     }
  • 相关阅读:
    Poj3295 tautology
    Poj2586 每五个月都是亏
    Poj 2109 k^n = p.
    Poj2109 (2) k^n = p.
    Poj2109 (1) k^n = p.
    Poj2965 冰箱的开关
    Poj1328 用雷达覆盖所有的岛屿
    FASTER-RCNN
    卷积、池化计算
    理论感受野的计算
  • 原文地址:https://www.cnblogs.com/Fred1987/p/11923135.html
Copyright © 2011-2022 走看看