【问题标题】:Pass CommandParameter to Command in Silverlight using MVVM使用 MVVM 在 Silverlight 中将 CommandParameter 传递给 Command
【发布时间】:2011-05-27 20:29:06
【问题描述】:

我只是在学习 Silverlight,正在研究 MVVM 和 Commanding。

好的,所以我已经看到了基本的 RelayCommand 实现:

public class RelayCommand : ICommand
{
    private readonly Action _handler;
    private bool _isEnabled;

    public RelayCommand(Action handler)
    {
        _handler = handler;
    }

    public bool IsEnabled
    {
        get { return _isEnabled; }
        set
        {
            if (value != _isEnabled)
            {
                _isEnabled = value;
                if (CanExecuteChanged != null)
                {
                    CanExecuteChanged(this, EventArgs.Empty);
                }
            }
        }
    }

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

    public event EventHandler CanExecuteChanged;

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

如何使用此命令通过命令向下传递参数?

我看到你可以像这样传递CommandParameter

<Button Command="{Binding SomeCommand}" CommandParameter="{Binding SomeCommandParameter}" ... />

在我的 ViewModel 中,我需要创建命令,但 RelayCommand 需要一个 Action 委托。我可以使用Action&lt;T&gt; 实现RelayCommand&lt;T&gt; - 如果可以,我该怎么做以及如何使用它?

谁能给我任何关于使用 MVVM 的 CommandParameters 的实际示例,这些示例不涉及使用 3rd 方库(例如 MVVM Light),因为我想在使用现有库之前完全理解它。

谢谢。

【问题讨论】:

    标签: mvvm silverlight-4.0 command relaycommand commandparameter


    【解决方案1】:
    public class Command : ICommand
    {
        public event EventHandler CanExecuteChanged;
    
        Predicate<Object> _canExecute = null;
        Action<Object> _executeAction = null;
    
        public Command(Predicate<Object> canExecute, Action<object> executeAction)
        {
            _canExecute = canExecute;
            _executeAction = executeAction;
        }
        public bool CanExecute(object parameter)
        {
            if (_canExecute != null)
                return _canExecute(parameter);
            return true;
        }
    
        public void UpdateCanExecuteState()
        {
            if (CanExecuteChanged != null)
                CanExecuteChanged(this, new EventArgs());
        }
    
        public void Execute(object parameter)
        {
            if (_executeAction != null)
                _executeAction(parameter);
            UpdateCanExecuteState();
        }
    }
    

    是命令的基类

    这是您在 ViewModel 中的命令属性:

     private ICommand yourCommand; ....
    
     public ICommand YourCommand
        {
            get
            {
                if (yourCommand == null)
                {
                    yourCommand = new Command(  //class above
                        p => true,    // predicate to check "CanExecute" e.g. my_var != null
                        p => yourCommandFunction(param1, param2));
                }
                return yourCommand;
            }
        }
    

    在 XAML 中设置绑定到命令属性,如:

     <Button Command="{Binding Path=YourCommand}" .../>
    

    【讨论】:

    • 谢谢 Tom - 你在 XAML 中使用 CommandParameter 来传递参数吗?
    • 不,我在 ViewModel 中使用该谓词
    【解决方案2】:

    也许这篇文章解释了你在寻找什么。几分钟前我也遇到了同样的问题。

    http://www.eggheadcafe.com/sample-code/SilverlightWPFandXAML/76e6b583-edb1-4e23-95f6-7ad8510c0f88/pass-command-parameter-to-relaycommand.aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-06-03
      • 1970-01-01
      • 2022-01-26
      • 1970-01-01
      • 2018-12-25
      • 2011-10-21
      • 2016-02-05
      • 2014-06-13
      相关资源
      最近更新 更多