■ ICommand 인터페이스에서 명령 명칭으로 실행하는 방법을 보여준다.
▶ ICommand 인터페이스 : 명령 명칭으로 실행하기 예제 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System.Windows.Input; #region 윈도우 마우스 DOWN 처리하기 - Window_MouseDown(sender, e) /// <summary> /// 윈도우 마우스 DOWN 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_MouseDown(object sender, MouseButtonEventArgs e) { StringCommand.Execute<MouseButtonEventArgs>(DataContext, "MouseDownCommand", e); } #endregion |
▶ ICommand 인터페이스 : 명령 명칭으로 실행하기 (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 |
using System.Reflection; using System.Windows.Input; /// <summary> /// 문자열 명령 /// </summary> public static class StringCommand { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 실행하기 - Execute<TParameter>(dataContext, commandName, parameter) /// <summary> /// 실행하기 /// </summary> /// <typeparam name="TParameter">매개 변수 타입</typeparam> /// <param name="dataContext">데이터 컨텍스트</param> /// <param name="commandName">명령 명칭</param> /// <param name="parameter">매개 변수</param> public static void Execute<TParameter>(object dataContext, string commandName, TParameter parameter) { if(dataContext == null) { return; } if(string.IsNullOrWhiteSpace(commandName)) { return; } PropertyInfo propertyInfo = dataContext.GetType().GetProperty(commandName); if(propertyInfo == null) { return; } ICommand command = propertyInfo.GetValue(dataContext, null) as ICommand; if(command == null) { return; } command.Execute(parameter); } #endregion #region 실행하기 - Execute(dataContext, command) /// <summary> /// 실행하기 /// </summary> /// <param name="dataContext">데이터 컨텍스트</param> /// <param name="command">명령</param> public static void Execute(object dataContext, string command) { Execute<object>(dataContext, command, null); } #endregion } |