【问题标题】:How To send Command Parameter In Mvvm Silverlight 5如何在 Mvvm Silverlight 5 中发送命令参数
【发布时间】:2013-03-13 06:58:41
【问题描述】:

我已经在我的 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'。

请帮助。提前致谢。

【问题讨论】:

    标签: xaml mvvm silverlight-5.0 icommand


    【解决方案1】:

    问题在于您的 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.
        }
      }
    

    【讨论】:

    • 谢谢它的工作原理..但现在我没有得到如何在我的视图模型中获取这个 CommandParameter="{Binding ElementName=grdReceipt}" .. private void ExecutePrintCommand(object o) { // 如何获取这里的参数值}
    • @Gayatri 是否作为参数传递给您的命令的方法 CanExecute 和 Execute。
    • :非常感谢..您的回答对我帮助很大。
    猜你喜欢
    • 2011-07-22
    • 1970-01-01
    • 2010-12-01
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-16
    • 1970-01-01
    相关资源
    最近更新 更多