【问题标题】:WPF Multibinding - Need to use RelaycommandWPF Multibinding - 需要使用 Relaycommand
【发布时间】:2015-09-20 23:20:57
【问题描述】:

所以,我有一个元素,它有一个带有 2 个参数的命令要传递。

我之前用我找到的一段 sn-p 代码做到了这一点,但我终生无法记住如何做到这一点或再次找到它。

所以,这里是我之前创建的多值转换器:

public class MultiValueConverter : IMultiValueConverter
{

    public object Convert(object[] values, Type targetType,
    object parameter, CultureInfo culture)
    {
        return values.Clone();
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        return (value as string).Split(' ');
    }

}

现在,我只需在 ICommand 中分配我想要调用的函数。我通常使用类似于以下的行:

enemyPopupTooltip = new RelayCommand(param => this.EnemyPopupTooltipEx(param),null);

但是,当它的多值时,这将不起作用。如何使用我的中继命令通过多值转换器将 2 个参数传递到我的函数中?

作为参考,这里是 relaycommand 类中的所有内容:

public class RelayCommand : ICommand
{
    /// <summary>
    /// Initializes a new instance of the <see cref="RelayCommand"/> class.
    /// </summary>
    /// <param name="execute">The execute.</param>
    public RelayCommand(Action<object> execute)
        : this(execute, null)
    {
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="RelayCommand"/> class.
    /// </summary>
    /// <param name="execute">The execute.</param>
    /// <param name="canExecute">The can execute.</param>
    public RelayCommand(Action<object> execute, Predicate<object> canExecute)
    {
        if (execute == null)
            throw new ArgumentNullException("execute");
        _execute = execute;
        _canExecute = canExecute;
    }

    /// <summary>
    /// Defines the method that determines whether the command can execute in its current state.
    /// </summary>
    /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
    /// <returns>
    /// true if this command can be executed; otherwise, false.
    /// </returns>
    public bool CanExecute(object parameter)
    {
        return _canExecute == null ? true : _canExecute(parameter);
    }

    /// <summary>
    /// Occurs when changes occur that affect whether or not the command should execute.
    /// </summary>
    public event EventHandler CanExecuteChanged
    {
        add { CommandManager.RequerySuggested += value; }
        remove { CommandManager.RequerySuggested -= value; }
    }

    /// <summary>
    /// Defines the method to be called when the command is invoked.
    /// </summary>
    /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
    public void Execute(object parameter)
    {
        _execute(parameter);
    }

    /// <summary>
    /// Action
    /// </summary>
    private readonly Action<object> _execute;


    /// <summary>
    /// Predicate
    /// </summary>
    private readonly Predicate<object> _canExecute;

【问题讨论】:

  • 那个链接的问题没有显示在 MyViewModel.ZoomCommand 中会写什么,这就是我正在努力解决的问题:(
  • 第二个方法:你不能将所需的参数绑定到某些 ViewModel 属性吗?然后你只需在你的方法中调用 VM 属性

标签: c# wpf mvvm multibinding relaycommand


【解决方案1】:

你说:

但是,当它的多值时,这将不起作用

这个假设是错误的。它确实有效!

当您的多转换器返回值数组时,该数组将作为参数传递给 Command.Execute 方法。

new RelayCommand(EnemyPopupTooltipEx, null);

public void EnemyPopupTooltipEx(object parameter){
   var values = (object[])parameters;
}

然而,这是非常肮脏的做法。我猜您正在将一些 UIElement(s) 传递给命令参数。这违反了 viewmodel 的责任。考虑将需要引用 UIElement 的代码移动到代码隐藏中。

【讨论】:

  • 我知道这是一种肮脏的做法,但在某些情况下我们必须这样做。例如。如果我想刷新我的数据网格,那么我们需要发送 UIElement 元素。
  • 您永远不需要将 UI 元素发送到视图模型,相信我。这是题外话,但如果您对如何做干净的方式感兴趣,请创建新问题并通过在此处添加评论来通知我。
  • 添加了一个新问题(如何使用 MVVM 刷新我的 DataGrid)
【解决方案2】:

只需将您的两个参数放入一个对象中。您可以使用任何类型的集合或数组,但也许最简单的选择是在您的IMultiValueConverter 中使用Tuple&lt;T1, T2&gt; Class

if (values != null && values.Length >= 2)
{
    Tuple<Type1, Type2> yourTwoValues = new Tuple<Type1, Type2>(values[0], values[1]);
    return yourTwoValues;
}

然后您可以将Tuple 作为参数传递给您的ICommand,并在另一端提取各个值。

【讨论】:

  • 好的,谢谢。在这种情况下,我的中继命令是否会与上面的相同?
  • 是的。 RelayCommand.Execute 方法有一个 object parameter 和一个 Tuple 是一个 object 所以你可以将它作为参数传递。
【解决方案3】:

尝试使用新的属性,在 CommandParameter 中执行 multibing 并在 ExecuteEnterCommand 中处理。喜欢object[] arr = (object[])obj;

 public ICommand EnemyPopupTooltip
        {
            get
            {
                if (this.enemyPopupTooltip == null)
                {
                    this.enemyPopupTooltip = new RelayCommand<object>(this.ExecuteEnterCommand, this.CanExecuteEnterCommand);
                }

                return this.enemyPopupTooltip;
            }
        }


        private ICommand enemyPopupTooltip;

【讨论】:

    猜你喜欢
    • 2018-12-25
    • 1970-01-01
    • 1970-01-01
    • 2015-04-28
    • 2012-11-22
    • 2011-03-09
    • 1970-01-01
    • 2019-03-02
    • 2011-08-29
    相关资源
    最近更新 更多