• 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     }
  • 相关阅读:
    不装.net Framework 也能运行WinForm程序,用飞信(转)
    hdu 1058 Humble Numbers
    c# winform 应用编程代码总结 2
    c# winform 应用编程代码总结 1
    c# winform 应用编程代码总结 4
    住在我隔壁储藏室的大学刚毕业的小夫妻(震撼,转贴)

    CEGUI 0.6.0 Released!
    my ogre plugin for 3dmax
    卡通渲染进阶 = toonlighting + outline + rimlighting + hair specular
  • 原文地址:https://www.cnblogs.com/Fred1987/p/11923135.html
Copyright © 2020-2023  润新知