[英]WPF Multibinding - Need to use 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的代码移到代码背后。
只需将两个参数放入一个对象。 您可以使用任何类型的集合或数组,但也许最简单的选择是在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
,并在另一端提取各个值。
尝试使用新属性,在CommandParameter中进行多路处理,并在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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.