gpt4 book ai didi

c# - WPF CommandParameter MultiBinding 值为 null

转载 作者:太空狗 更新时间:2023-10-29 19:59:59 25 4
gpt4 key购买 nike

我只是想将两个控件绑定(bind)为命令参数,并将它们作为 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.");
}
}

基本上发生的事情是绑定(bind)似乎工作正常并且转换器返回一个包含正确值的 object[],但是当命令执行时参数是一个 object [] 包含相同数量的元素,但它们是 null

有人能告诉我为什么 object[] 参数的值被设置为 null 吗?

谢谢,亚历克斯。

最佳答案

这样就可以了:

public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
return values.ToArray();
}

看看这个question求解释。

关于c# - WPF CommandParameter MultiBinding 值为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13713814/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com