我在我的mvvm架构中实现了Icommand为SimpleCommand.cs
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);
}
}
}
在我的viewModel中实现了这个ICommand如下:
private ICommand printCommand;
public ICommand PrintCommand
{
get { return printCommand; }
set { printCommand = value; }
}
myviewmodel() // in Constructor of ViewModel
{
this.PrintCommand = new SimpleCommand<Object, EventToCommandArgs>(ExecutePrintCommand);
}
}
在XAML上:我已经调用Command =“{Binding 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>
然后Command没有执行。 并给出错误:
无法将类型为“System.Windows.Controls.Grid”的对象强制转换为类型 'PropMgmt.Shared.EventToCommandArgs'。
请帮助。谢谢。
答案 0 :(得分:2)
问题在于SimpleCommand实现中的这部分
public void Execute(object parameter){
Execute((T2)parameter);
}
您的案例中的 T2
类型为EventToCommandArgs
,但作为参数
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);
}
}
}
使用此实现,您可以定义:
this.PrintCommand = new SimpleCommand<object>(ExecutePrintCommand);
由于您的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.
}
}