나는 SimpleCommand.csMVVM 실버 라이트에서 명령 매개 변수를 보내려면 어떻게 5
public class SimpleCommand<T1, T2> : ICommand
{
private Func<T1, bool> canExecuteMethod;
private Action<T2> executeMethod;
public SimpleCommand(Func<T1, bool> canExecuteMethod, Action<T2> executeMethod)
{
this.executeMethod = executeMethod;
this.canExecuteMethod = canExecuteMethod;
}
public SimpleCommand(Action<T2> executeMethod)
{
this.executeMethod = executeMethod;
this.canExecuteMethod = (x) => { return true; };
}
public bool CanExecute(T1 parameter)
{
if (canExecuteMethod == null) return true;
return canExecuteMethod(parameter);
}
public void Execute(T2 parameter)
{
if (executeMethod != null)
{
executeMethod(parameter);
}
}
public bool CanExecute(object parameter)
{
return CanExecute((T1)parameter);
}
public void Execute(object parameter)
{
Execute((T2)parameter);
}
public event EventHandler CanExecuteChanged;
public void RaiseCanExecuteChanged()
{
var handler = CanExecuteChanged;
if (handler != null)
{
handler(this, EventArgs.Empty);
}
}
}
로 내 MVVM 아키텍처에 ICommand의 구현 그리고 내 뷰 모델로이 ICommand의 구현은 다음과 적이 : XAML에
private ICommand printCommand;
public ICommand PrintCommand
{
get { return printCommand; }
set { printCommand = value; }
}
myviewmodel() // in Constructor of ViewModel
{
this.PrintCommand = new SimpleCommand<Object, EventToCommandArgs>(ExecutePrintCommand);
}
}
: 나는 명령 = "{PrintCommand 바인딩"}
<Button Content="Print Button" Command="{Binding PrintCommand}" Width="120" Height="25" Margin="3"></Button>
그것은 완벽하게 작동합니다 ...
그러나 나는 명령으로 CommandParameter를 보내려고 할 때 :
<Button Content="Print Button" Command="{Binding PrintCommand}" Width="120" Height="25" Margin="3" CommandParameter="{Binding ElementName=grdReceipt}"></Button>
다음 명령이 실행되지 않습니다. 와 같이주기 오류 :
유형의 개체를 캐스팅 할 수 없습니다가 'System.Windows.Controls.Grid' 'PropMgmt.Shared.EventToCommandArgs'을 입력합니다.
제발 도와주세요. 미리 감사드립니다.
덕분에 내가이 CommandParameter을 얻는 방법을 얻을 didnt는 지금 ..하지만 작동 = "{바인딩 ElementName을 = grdReceipt}"내 뷰 모델에 .. 개인 무효 ExecutePrintCommand (객체 O) {// 값을 얻는 방법 of parameter here} – Gayatri
@Gayatri 명령의 CanExecute 및 Execute 메서드에 매개 변수로 전달됩니까? – Jehof
@Gayatri 나는 내 대답을 업데이트했습니다 – Jehof