728x90
728x170

datacontext 의 특정 Command 를 실행하는 메서드로 Parameter 를 인자로 전달 할수 있습니다.


using System.Reflection;

using System.Windows.Input;


namespace WpfApp

{

    class TestClass

    {


        /// <summary>

        /// DataContext 의 특정 Command 를 파라미터와 함께 실행합니다.

        /// </summary>

        /// <typeparam name="T">파라미터 타입입니다.</typeparam>

        /// <param name="dataContext">DataContext 입니다.</param>

        /// <param name="command">Command 명입니다.</param>

        /// <param name="parameter">파라미터입니다.</param>

        public static void Execute<T>(object dataContext, string command, T parameter)

        {

            if (dataContext == null)

            {

                return;

            }


            if (string.IsNullOrWhiteSpace(command) == true)

            {

                return;

            }


            // Command 명으로 찾습니다.

            PropertyInfo pi = dataContext.GetType().GetProperty(command);

            if (pi == null)

            {

                return;

            }


            // Command 로 변환합니다.

            ICommand ic = pi.GetValue(dataContext, null) as ICommand;

            if (ic == null)

            {

                return;

            }


            // Command 를 호출합니다.

            ic.Execute(parameter);

        }

    }

}




728x90
그리드형
Posted by kjun
,