Xaml 如何在Mvvm Silverlight 5中发送命令参数

Xaml 如何在Mvvm Silverlight 5中发送命令参数,xaml,mvvm,silverlight-5.0,icommand,Xaml,Mvvm,Silverlight 5.0,Icommand,我已经在我的mvvm体系结构中以SimpleCommand.cs的形式实现了Icommand public class SimpleCommand<T1, T2> : ICommand { private Func<T1, bool> canExecuteMethod; private Action<T2> executeMethod; public SimpleCommand(Func<T1, bool> canExe

我已经在我的mvvm体系结构中以SimpleCommand.cs的形式实现了Icommand

 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);
        }
    }
}
公共类SimpleCommand:ICommand
{
私人职能执行方法;
私人行动执行方法;
公共SimpleCommand(Func canExecuteMethod、Action executeMethod)
{
this.executeMethod=executeMethod;
this.canExecuteMethod=canExecuteMethod;
}
公共SimpleCommand(操作执行方法)
{
this.executeMethod=executeMethod;
this.canExecuteMethod=(x)=>{return true;};
}
公共布尔CanExecute(T1参数)
{
if(canExecuteMethod==null)返回true;
返回canExecuteMethod(参数);
}
public void Execute(T2参数)
{
if(executeMethod!=null)
{
executeMethod(参数);
}
}
公共布尔CanExecute(对象参数)
{
返回CanExecute((T1)参数);
}
public void Execute(对象参数)
{
执行((T2)参数);
}
公共事件处理程序CanExecuteChanged;
public void raisecancecutechanged()
{
var handler=CanExecuteChanged;
if(处理程序!=null)
{
处理程序(此,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);
}

 }
private ICommand printCommand;
公共ICommand print命令
{
获取{return printCommand;}
设置{printCommand=value;}
}
myviewmodel()//在ViewModel的构造函数中
{
this.PrintCommand=newsimpleCommand(ExecutePrintCommand);
}
}
在XAML上:我调用了Command=“{Binding PrintCommand}”


它工作得很好

但当我尝试将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”


请帮助。提前感谢。

问题在于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);
        }
    }
}

感谢它能工作..但是现在我不知道如何在我的viewmodel中获取这个CommandParameter=“{Binding ElementName=grdReceipt}”。。private void ExecutePrintCommand(object o){//how to get value of parameter here}@Gayatri它是否作为参数传递给可以执行命令的方法。:非常感谢。。你的回答对我帮助很大。
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);
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.
    }
  }