■ DelegateCommand 클래스를 사용하는 방법을 보여준다.
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
using System.Windows.Input; using DevExpress.Xpf.Mvvm; /// <summary> /// 바인딩 가능한 객체 /// </summary> public class BindableObject : BindableBase { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 정수 속성 /// </summary> private int integerProperty; /// <summary> /// 명령 1 /// </summary> private ICommand command1; /// <summary> /// 명령 2 /// </summary> private ICommand command2; /// <summary> /// 명령 3 /// </summary> private ICommand command3; /// <summary> /// 명령 4 /// </summary> private ICommand command4; /// <summary> /// 명령 5 /// </summary> private DelegateCommand command5; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 정수 속성 - IntegerProperty /// <summary> /// 정수 속성 /// </summary> public int IntegerProperty { get { return this.integerProperty; } set { SetProperty(ref this.integerProperty, value, () => IntegerProperty, () => Command5.RaiseCanExecuteChanged()); } } #endregion #region 명령 1 - Command1 /// <summary> /// 명령 1 /// </summary> public ICommand Command1 { get { if(this.command1 == null) { this.command1 = new DelegateCommand(OnCommand1Execute); } return this.command1; } } #endregion #region 명령 2 - Command2 /// <summary> /// 명령 2 /// </summary> public ICommand Command2 { get { if(command2 == null) { this.command2 = new DelegateCommand(OnCommand2Execute, OnCommand2CanExecute); } return this.command2; } } #endregion #region 명령 3 - Command3 /// <summary> /// 명령 3 /// </summary> public ICommand Command3 { get { if(this.command3 == null) { this.command3 = new DelegateCommand<object>(OnCommand3Execute, OnCommand3CanExecute); } return this.command3; } } #endregion #region 명령 4 - Command4 /// <summary> /// 명령 4 /// </summary> public ICommand Command4 { get { if(this.command4 == null) { this.command4 = new DelegateCommand<int>(OnCommand4Execute, OnCommand4CanExecute); } return this.command4; } } #endregion // 정수 속성 값 설정시 명령 5 실행 가능 여부 조사 요청 처리를 위해 ICommand가 아닌 DelegateCommand로 정의된다. #region 명령 5 - Command5 /// <summary> /// 명령 5 /// </summary> public DelegateCommand Command5 { get { if(this.command5 == null) { this.command5 = new DelegateCommand(OnCommand5Execute, OnCommand5CanExecute); } return this.command5; } } #endregion // 생성자에서 초기화 한다. #region 명령 6 - Command6 /// <summary> /// 명령 6 /// </summary> public ICommand Command6 { get; private set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - BindableObject() /// <summary> /// 생성자 /// </summary> public BindableObject() { Command6 = new DelegateCommand(() => { /* 처리 코드 */ ; }); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 명령 1 실행시 처리하기 - OnCommand1Execute() /// <summary> /// 명령 1 실행시 처리하기 /// </summary> private void OnCommand1Execute() { } #endregion #region 명령 2 실행시 처리하기 - OnCommand2Execute() /// <summary> /// 명령 2 실행시 처리하기 /// </summary> private void OnCommand2Execute() { } #endregion #region 명령 2 실행 가능 여부 조사시 처리하기 - OnCommand2CanExecute() /// <summary> /// 명령 2 실행 가능 여부 조사시 처리하기 /// </summary> /// <returns>명령 2 실행 가능 여부</returns> private bool OnCommand2CanExecute() { return true; } #endregion #region 명령 3 실행시 처리하기 - OnCommand3Execute(parameter) /// <summary> /// 명령 3 실행시 처리하기 /// </summary> /// <param name="parameter">매개 변수</param> private void OnCommand3Execute(object parameter) { } #endregion #region 명령 3 실행 가능 여부 조사시 처리하기 - OnCommand3CanExecute(parameter) /// <summary> /// 명령 3 실행 가능 여부 조사시 처리하기 /// </summary> /// <param name="parameter">매개 변수</param> /// <returns>명령 3 실행 가능 여부</returns> private bool OnCommand3CanExecute(object parameter) { return true; } #endregion #region 명령 4 실행시 처리하기 - OnCommand4Execute(parameter) /// <summary> /// 명령 4 실행시 처리하기 /// </summary> /// <param name="parameter">매개 변수</param> private void OnCommand4Execute(int parameter) { } #endregion #region 명령 4 실행 가능 여부 조사시 처리하기 - OnCommand4CanExecute(parameter) /// <summary> /// 명령 4 실행 가능 여부 조사시 처리하기 /// </summary> /// <param name="parameter">매개 변수</param> /// <returns>명령 4 실행 가능 여부</returns> private bool OnCommand4CanExecute(int parameter) { return true; } #endregion #region 명령 5 실행시 처리하기 - OnCommand5Execute() /// <summary> /// 명령 5 실행시 처리하기 /// </summary> private void OnCommand5Execute() { } #endregion #region 명령 5 실행 가능 여부 조사시 처리하기 - OnCommand5CanExecute() /// <summary> /// 명령 5 실행 가능 여부 조사시 처리하기 /// </summary> /// <returns>명령 5 실행 가능 여부</returns> private bool OnCommand5CanExecute() { return IntegerProperty % 2 == 0; } #endregion } |