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 }
原文链接: https://www.cnblogs.com/Fred1987/p/11923135.html
欢迎关注
微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍;
也有高质量的技术群,里面有嵌入式、搜广推等BAT大佬
原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/402099
非原创文章文中已经注明原地址,如有侵权,联系删除
关注公众号【高性能架构探索】,第一时间获取最新文章
转载文章受原作者版权保护。转载请注明原作者出处!