2

我已经在我的 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 发送到 Command As 时:

<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”类型。

请帮助。在此先感谢。

4

1 回答 1

2

问题是您的 SimpleCommand 实现中的这一部分

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

T2在你的情况下是 type EventToCommandArgs,但作为参数

CommandParameter="{Binding ElementName=grdReceipt}"

您正在传递 a 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.
    }
  }
于 2013-03-13T07:58:17.150 回答