WPF CommandParameter MultiBinding值空(WPF CommandPar

2019-07-04 13:05发布

我只是想给两个控件绑定的命令参数,并将其传递到我的命令作为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.");
        }
    }

基本上所发生的事情是,结合似乎是正常,并且转换器返回一个object[]含有正确的值,然而,当该命令执行该参数是一个object[]含有相同数量的元素,除了它们是null

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

谢谢,亚历克斯。

Answer 1:

这会做到这一点:

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

看看这个问题的解释。



文章来源: WPF CommandParameter MultiBinding values null