WPF多绑定-需要使用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);

然而,当它是多值的时,这就不起作用了。如何使用relaycommand将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;

您说过:

然而,当它的多值

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

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

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

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

只需将两个参数放入一个对象中。您可以使用任何类型的集合或数组,但最简单的选择可能是在IMultiValueConverter:中使用Tuple<T1, T2>

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

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

尝试使用新的Property,在CommandParameter中执行multibing,在ExecuteEnterCommand中执行Handle。如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;

相关内容

  • 没有找到相关文章

最新更新