c# - WPF CommandParameter MultiBinding 值为 null

标签 c# wpf binding .net-4.0 multibinding

我只是想将两个控件绑定(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/

相关文章:

c# - 悬停 ComboBox 时更改 ComboBoxItem 的前景色

c# - 如何在 WPF 中使用每张多页的 DocumentViewer 预览文档

c# - 报告异步任务的进度

c# - ASP.NET Web 服务中的安全性,只允许特定的人使用它

wpf - 覆盖 StyleTypedProperty 属性

c# - 我应该绑定(bind)到 WPF Prism 中的 SharedService 属性吗

c# - MVVM - 从 View 模型关闭一个窗口

.net - 将 DataContext 绑定(bind)到 ValidationRule

c# - 使用 System.DirectoryServices.AccountManagement 时 Active Directory 用户创建延迟

c# - MVC 5/ASP.Net 4.5 安全地存储连接字符串