【发布时间】:2020-01-04 18:53:48
【问题描述】:
我有一个Prism MVVM 应用程序,可以轻松地将一个UIElement 作为CommandParameter 传递给ViewModel 的Command。但现在我想传递两个 UIElement。使用此 XAML:
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource DummyMultiConverter}">
<Binding ElementName="PasswordBoxType"/>
<Binding ElementName="PasswordBoxRetype"/>
</MultiBinding>
</Button.CommandParameter>
使用这个Converter:
public class DummyMultiConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return values;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
还有一个简单的 DelegateCommand 在 ViewModel 中:
private DelegateCommand<object> _commandCloseDialogOK;
public DelegateCommand<object> CommandCloseDialogOK =>
_commandCloseDialogOK ??
(_commandCloseDialogOK = new DelegateCommand<object>(commandParameter=> CommandCloseDialogOKExecute(commandParameter)));
public virtual void CommandCloseDialogOKExecute(object commandParameter)
{
RaiseRequestClose(new DialogResult(ButtonResult.OK));
}
public virtual bool CanExecuteCommandCloseDialogOK()
{
return true;
}
当它运行时 - Convert 方法正确获取 values,作为 2 个 PasswordBoxes 的数组。但是 CommandCloseDialogOKExecute 将其 commandParameter 参数作为两个 nulls 的数组获取。如果我将 commandParameter 定义为 object[] 而不是 object,也会发生同样的情况。我应该怎么做 commandParameter 将是两个 PasswordBoxes 的数组?
【问题讨论】: