本文介绍了WPF CommandParameter MultiBinding值空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我只是想结合两个控件命令参数,并将它们传递到我的命令作为 [对象]
。
I am simply trying to bind two controls as command parameters and pass them into my command as an object[]
.
XAML:
<UserControl.Resources>
<C:MultiValueConverter x:Key="MultiParamConverter"></C:MultiValueConverter>
</UserControl.Resources>
<StackPanel Orientation="Vertical">
<StackPanel Orientation="Horizontal">
<Button Name="Expander" Content="+" Width="25" Margin="4,0,4,0" Command="{Binding ExpanderCommand}">
<Button.CommandParameter>
<MultiBinding Converter="{StaticResource MultiParamConverter}">
<Binding ElementName="Content"/>
<Binding ElementName="Expander"/>
</MultiBinding>
</Button.CommandParameter>
</Button>
<Label FontWeight="Bold">GENERAL INFORMATION</Label>
</StackPanel>
<StackPanel Name="Content" Orientation="Vertical" Visibility="Collapsed">
<Label>Test</Label>
</StackPanel>
</StackPanel>
命令:
public ICommand ExpanderCommand
{
get
{
return new RelayCommand(delegate(object param)
{
var args = (object[])param;
var content = (UIElement)args[0];
var button = (Button)args[1];
content.Visibility = (content.Visibility == Visibility.Visible) ? Visibility.Collapsed : Visibility.Visible;
button.Content = (content.Visibility == Visibility.Visible) ? "-" : "+";
});
}
}
转换:
public class MultiValueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return values;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException("No two way conversion, one way binding only.");
}
}
基本上正在发生的事情是,结合似乎是工作的罚款,该转换器返回一个 [对象]
包含正确的价值观,但是,当命令执行参数是[] 包含元素相同数量的的对象,除非它们是
空
。
Basically what is happening is that the binding seems to be working fine and the converter is returning an object[]
containing the correct values, however when the command executes the param is an object[]
containing the same number of elements except they are null
.
有人能告诉我,为什么 [对象]
参数被设置为值空
?
Can someone please tell me why the values of the object[]
parameter are being set to null
?
谢谢,亚历克斯。
推荐答案
这会做到这一点:
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return values.ToArray();
}
看看这个question为解释
这篇关于WPF CommandParameter MultiBinding值空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!