效果图如下:支持分组的单选框,复选框样式和MVVM下功能的实现。这是项目中一个快捷键功能的扩展。
1,准备工作:VS2015 (15对WPF的支持变得异常的好,调试模式下允许自动更改属性。),随VS发布的Blend,几个基础类:
1 public classRelayCommand : ICommand2 {3 #region Fields
4
5 readonly Action_executeAct;6 readonly Predicate_canExecutePre;7 private readonlyAction _execute;8
9 private readonly Func_canExecute;10 #endregion
11
12 #region Constructors
13
14 ///
15 ///Creates a new command that can always execute.16 ///
17 /// The execution logic.
18 public RelayCommand(Actionexecute)19 : this(execute, null)20 {21 }22
23 ///
24 ///Creates a new command.25 ///
26 /// The execution logic.
27 /// The execution status logic.
28 public RelayCommand(Action execute, PredicatecanExecute)29 {30 if (execute == null)31 {32 throw new ArgumentNullException("execute");33 }34
35 _executeAct =execute;36 _canExecutePre =canExecute;37 }38
39
40 ///
41 ///Initializes a new instance of the RelayCommand class that42 ///can always execute.43 ///
44 /// The execution logic.
45 /// If the execute argument is null.
46 publicRelayCommand(Action execute)47 : this(execute, null)48 {49 }50
51 ///
52 ///Initializes a new instance of the RelayCommand class.53 ///
54 /// The execution logic.
55 /// The execution status logic.
56 /// If the execute argument is null.
57 public RelayCommand(Action execute, FunccanExecute)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(objectparameter)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 eventEventHandler CanExecuteChanged83 {84 add { CommandManager.RequerySuggested +=value; }85 remove { CommandManager.RequerySuggested -=value; }86 }87
88 public void Execute(objectparameter)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 }
View Cod