WPF CommandParameter MultiBinding 值 null

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13713814/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:28:27  来源:igfitidea点击:

WPF CommandParameter MultiBinding values null

c#wpfbinding.net-4.0multibinding

提问by Alex Hope O'Connor

I am simply trying to bind two controls as command parameters and pass them into my command as an object[].

我只是想将两个控件绑定为命令参数,并将它们作为object[].

XAML:

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>

Command:

命令:

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) ? "-" : "+";
                    });
            }
        }

Converter:

转换器:

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.

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

Can someone please tell me why the values of the object[]parameter are being set to null?

有人可以告诉我为什么将object[]参数的值设置为null

Thanks, Alex.

谢谢,亚历克斯。

回答by dzavala

This'll do it:

这样做:

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

Take a look at this questionfor the explanation.

看看这个问题的解释。