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);
}
}
}
'C# > WPF' 카테고리의 다른 글
[WPF] UpdateSourceTrigger (0) | 2020.09.07 |
---|---|
[WPF] TextBox 에 PlaceHolder 설정하기 (0) | 2020.09.04 |
[WPF] 이미지를 Rect 기준으로 잘라내는 함수 (0) | 2020.08.20 |
[WPF] MultiBinding 사용하기 (0) | 2020.08.04 |
[WPF] ContextMenu 선택 후 ContextMenu 숨겨지지 않게 하기 (0) | 2020.08.03 |