2013-03-13 6 views
2

나는 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'을 입력합니다.

제발 도와주세요. 미리 감사드립니다.

답변

2

문제는 귀하의 경우

public void Execute(object parameter){ 
    Execute((T2)parameter); 
} 

T2 유형 EventToCommandArgs의 당신의 SimpleCommand 구현이 일부이지만 매개 변수로

CommandParameter="{Binding ElementName=grdReceipt}" 

당신은 예외 결과 상위 System.Windows.Controls.Grid을 전달하는 너는 언급했다.

또한 명령을 잘못 구현 한 것입니다. CanExecute 및 Execute에 전달 된 매개 변수는 동일한 개체입니다. 다음 구현은 하나의 제네릭 형식 만 사용하여 작동합니다.

public class SimpleCommand<T1> : ICommand 
{ 
    private Func<T1, bool> canExecuteMethod; 
    private Action<T1> executeMethod; 

    public SimpleCommand(Func<T1, bool> canExecuteMethod, Action<T1> executeMethod) 
    { 
     this.executeMethod = executeMethod; 
     this.canExecuteMethod = canExecuteMethod; 
    } 

    public SimpleCommand(Action<T1> 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(T1 parameter) 
    { 
     if (executeMethod != null) 
     { 
      executeMethod(parameter); 
     } 
    } 

    public bool CanExecute(object parameter) 
    { 
     return CanExecute((T1)parameter); 
    } 

    public void Execute(object parameter) 
    { 
     Execute((T1)parameter); 
    } 

    public event EventHandler CanExecuteChanged; 

    public void RaiseCanExecuteChanged() 
    { 
     var handler = CanExecuteChanged; 
     if (handler != null) 
     { 
      handler(this, EventArgs.Empty); 
     } 
    } 
} 

정의 할 수있는이 구현을 사용 : 당신의 ExecutePrintCommand가 매개 변수로 개체를 가져옵니다

this.PrintCommand = new SimpleCommand<object>(ExecutePrintCommand); 

때문에 당신이 유형 확인을하거나 당신이 필요로하는 형식으로 캐스팅 할 필요가있다. 귀하의 경우에는 System.Windows.Controls.Grid이 표시됩니다.

public void ExecutPrintCommand(object parameter) { 
    System.Windows.Controls.Grid grid = parameter as System.Windows.Controls.Grid; 

    if (grid != null) { 
     // do something with the Grid 
    } 
    else { 
     // throw exception or cast to another type you need if your command should support more types. 
    } 
    } 
+0

덕분에 내가이 CommandParameter을 얻는 방법을 얻을 didnt는 지금 ..하지만 작동 = "{바인딩 ElementName을 = grdReceipt}"내 뷰 모델에 .. 개인 무효 ExecutePrintCommand (객체 O) {// 값을 얻는 방법 of parameter here} – Gayatri

+0

@Gayatri 명령의 CanExecute 및 Execute 메서드에 매개 변수로 전달됩니까? – Jehof

+0

@Gayatri 나는 내 대답을 업데이트했습니다 – Jehof