简体   繁体   English

WPF多重绑定-需要使用Relaycommand

[英]WPF Multibinding - Need to use Relaycommand

So, I have an element which has a command with 2 parameters to pass. 因此,我有一个具有2个参数要传递的命令的元素。

I previously did this with a snippet of code I found, but cannot for the life of me remember how to do it or find it again. 以前,我只是用发现的一小段代码来完成此操作,但是我一生都无法记住该怎么做或再次找到它。

So, here is the multivalueconverter I previously created: 因此,这是我之前创建的多值转换器:

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(' ');
    }

}

Now, I need to just assign the function I want to call in the ICommand. 现在,我只需要分配要在ICommand中调用的函数。 I normally use a line similar to: 我通常使用类似于以下内容的行:

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

However, this wont work when its multivalue.How can I use my relaycommand to pass 2 parameters, using the multivalueconverter, into my function? 但是,这在其多值时将不起作用。如何使用多值转换器将我的relaycommand传递2个参数到我的函数中?

For reference, here is everything inside relaycommand class: 作为参考,这是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;

you said: 你说:

However, this wont work when its multivalue 但是,当其多值时,这将不起作用

This assumption is wrong. 这个假设是错误的。 It does work! 确实有效!

When your multiconverter returns array of values, then this array is passed as a parameter to Command.Execute method. 当您的多转换器返回值数组时,该数组将作为参数传递给Command.Execute方法。

new RelayCommand(EnemyPopupTooltipEx, null);

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

however, this is very dirty approach. 但是,这是非常肮脏的方法。 I guess you are passing some UIElement(s) to the command parameter. 我猜您正在将一些UIElement传递给命令参数。 This is violation of viewmodel's responsibility. 这违反了viewmodel的责任。 Consider moving code that need reference to UIElement to codebehind. 考虑将需要引用UIElement的代码移到代码背后。

Just put your two parameters into one object. 只需将两个参数放入一个对象。 You could use any kind of collection or array, but perhaps the simplest option is to use the Tuple<T1, T2> Class in your IMultiValueConverter : 您可以使用任何类型的集合或数组,但也许最简单的选择是在IMultiValueConverter使用Tuple<T1, T2>

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

Then you can pass the Tuple to your ICommand as the parameter and extract the individual values at the other end. 然后,您可以将Tuple作为参数传递给ICommand ,并在另一端提取各个值。

Try with new Property, Do multibing in CommandParameter and Handle in ExecuteEnterCommand. 尝试使用新属性,在CommandParameter中进行多路处理,并在ExecuteEnterCommand中进行处理。 like object[] arr = (object[])obj; 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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM