WPF Multibinding - 需要使用 Relaycommand
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);
但是,当它 multivalue.How 我可以使用我的中继命令通过多值转换器将 2 个参数传递到我的函数中吗?
作为参考,这里是中继命令中的所有内容 class:
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;
只需将您的两个参数放入一个对象中即可。您可以使用任何类型的集合或数组,但也许最简单的选择是在 IMultiValueConverter
:
中使用 Tuple<T1, T2>
Class
if (values != null && values.Length >= 2)
{
Tuple<Type1, Type2> yourTwoValues = new Tuple<Type1, Type2>(values[0], values[1]);
return yourTwoValues;
}
然后您可以将 Tuple
作为参数传递给您的 ICommand
并在另一端提取各个值。
你说:
However, this wont work when its multivalue
这个假设是错误的。它确实有效!
当您的多转换器 returns 值数组时,此数组将作为参数传递给 Command.Execute 方法。
new RelayCommand(EnemyPopupTooltipEx, null);
public void EnemyPopupTooltipEx(object parameter){
var values = (object[])parameters;
}
然而,这是非常肮脏的做法。我猜你正在将一些 UIElement(s) 传递给命令参数。这违反了视图模型的责任。考虑将需要引用 UIElement 的代码移动到代码隐藏。
尝试使用新的 属性,在 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;
所以,我有一个元素,它有一个要传递 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);
但是,当它 multivalue.How 我可以使用我的中继命令通过多值转换器将 2 个参数传递到我的函数中吗?
作为参考,这里是中继命令中的所有内容 class:
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;
只需将您的两个参数放入一个对象中即可。您可以使用任何类型的集合或数组,但也许最简单的选择是在 IMultiValueConverter
:
Tuple<T1, T2>
Class
if (values != null && values.Length >= 2)
{
Tuple<Type1, Type2> yourTwoValues = new Tuple<Type1, Type2>(values[0], values[1]);
return yourTwoValues;
}
然后您可以将 Tuple
作为参数传递给您的 ICommand
并在另一端提取各个值。
你说:
However, this wont work when its multivalue
这个假设是错误的。它确实有效!
当您的多转换器 returns 值数组时,此数组将作为参数传递给 Command.Execute 方法。
new RelayCommand(EnemyPopupTooltipEx, null);
public void EnemyPopupTooltipEx(object parameter){
var values = (object[])parameters;
}
然而,这是非常肮脏的做法。我猜你正在将一些 UIElement(s) 传递给命令参数。这违反了视图模型的责任。考虑将需要引用 UIElement 的代码移动到代码隐藏。
尝试使用新的 属性,在 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;