原地址: http://www.cnblogs.com/yk250/p/5660340.html
效果图如下:支持分组的单选框,复选框样式和MVVM下功能的实现。这是项目中一个快捷键功能的扩展。
1,准备工作:VS2015 (15对WPF的支持变得异常的好,调试模式下允许自动更改属性。),随VS发布的Blend,几个基础类:
1 public class RelayCommand : ICommand 2 { 3 #region Fields 4 5 readonly Action<object> _executeAct; 6 readonly Predicate<object> _canExecutePre; 7 private readonly Action _execute; 8 9 private readonly Func<bool> _canExecute; 10 #endregion 11 12 #region Constructors 13 14 /// <summary> 15 /// Creates a new command that can always execute. 16 /// </summary> 17 /// <param name="execute">The execution logic.</param> 18 public RelayCommand(Action<object> execute) 19 : this(execute, null) 20 { 21 } 22 23 /// <summary> 24 /// Creates a new command. 25 /// </summary> 26 /// <param name="execute">The execution logic.</param> 27 /// <param name="canExecute">The execution status logic.</param> 28 public RelayCommand(Action<object> execute, Predicate<object> canExecute) 29 { 30 if (execute == null) 31 { 32 throw new ArgumentNullException("execute"); 33 } 34 35 _executeAct = execute; 36 _canExecutePre = canExecute; 37 } 38 39 40 /// <summary> 41 /// Initializes a new instance of the RelayCommand class that 42 /// can always execute. 43 /// </summary> 44 /// <param name="execute">The execution logic.</param> 45 /// <exception cref="ArgumentNullException">If the execute argument is null.</exception> 46 public RelayCommand(Action execute) 47 : this(execute, null) 48 { 49 } 50 51 /// <summary> 52 /// Initializes a new instance of the RelayCommand class. 53 /// </summary> 54 /// <param name="execute">The execution logic.</param> 55 /// <param name="canExecute">The execution status logic.</param> 56 /// <exception cref="ArgumentNullException">If the execute argument is null.</exception> 57 public RelayCommand(Action execute, Func<bool> canExecute) 58 { 59 if (execute == null) 60 { 61 throw new ArgumentNullException("execute"); 62 } 63 64 _execute = execute; 65 _canExecute = canExecute; 66 } 67 68 #endregion 69 70 #region ICommand Members 71 72 public bool CanExecute(object parameter) 73 { 74 if (parameter == null) 75 { 76 return _canExecute == null ? true : _canExecute(); 77 } 78 79 return _canExecutePre == null ? true : _canExecutePre(parameter); 80 } 81 82 public event EventHandler CanExecuteChanged 83 { 84 add { CommandManager.RequerySuggested += value; } 85 remove { CommandManager.RequerySuggested -= value; } 86 } 87 88 public void Execute(object parameter) 89 { 90 if (!CanExecute(parameter)) 91 return; 92 if (parameter == null) 93 { 94 if (_execute != null) 95 _execute(); 96 return; 97 } 98 if (_executeAct != null) 99 _executeAct(parameter); 100 101 } 102 103 #endregion 104 }
1 public class ModelBase : INotifyPropertyChanged 2 { 3 /// <summary> 4 /// 属性改变事件 5 /// </summary> 6 public event PropertyChangedEventHandler PropertyChanged; 7 8 /// <summary> 9 /// 触发属性改变 10 /// </summary> 11 /// <param name="propertyName"></param> 12 protected virtual void RaisePropertyChangedEvent(string propertyName) 13 { 14 ///一般写法 15 // PropertyChangedEventHandler handler = this.PropertyChanged; 16 // if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName)); 17 ///简易写法 18 PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 19 } 20 }
可分组的选择框控件(MVVM下)(Toggle样式 仿造单选框RadioButton,复选框CheckBox功能)
原文:http://www.cnblogs.com/yk250/p/5660340.html