c# - WPF Multibinding - 需要使用 Relay 命令

标签 c# wpf mvvm multibinding relaycommand

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

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

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

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;

最佳答案

你说:

However, this wont work when its multivalue



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

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

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

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

关于c# - WPF Multibinding - 需要使用 Relay 命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31201343/

相关文章:

c# - 如何在 WriteJson 期间删除属性

c# - Show 和 ShowDialog 将不起作用

c# - MEF 导入为空

javascript - 在javascript中读取cookie,由asp.net c#code制作

c# - WPF View 在关闭时将 ViewModel 属性设置为 null

.net - WPF 数据模板 ItemsControl 指定初始值和最终值

c# - 双字符串转换

c# - 如何在不在 wpf 中引入新引用的情况下从 View 模型 (.cs) 调用窗口 (.xaml.cs) 中的方法

c# - 检查来自不同线程的异步任务的完成

c# - 如何使用Delegatecommand.ObservesProperty观察多个属性