为什么这个多绑定(multibinding)不起作用?

8

我正在尝试从我的复选框命令中发送多个参数。我已经使用了一个转换器。代码如下。如果我放置一个调试器并查看这里的值,以下是我的结果:

当复选框选中或取消选中时:

在转换器中,它具有项目对象数组和布尔值的值。但当我到达我的方法时,该值是一个object [2],但两个值均为空

CheckBox XAML

 <CheckBox x:Name="checkBox" 
              Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content.Data.Label}"   
              ClickMode="Release"
              Command="{Binding Path=DataContext.SelectUnSelect}">
        <CheckBox.CommandParameter>
            <MultiBinding Converter="{StaticResource SelectedItemConverter}">
                <Binding RelativeSource="{RelativeSource TemplatedParent}" Path="Content.Data"/>
                <Binding RelativeSource="{RelativeSource Self}" Path="IsChecked"/>
            </MultiBinding>
        </CheckBox.CommandParameter>

转换器:

 public class CheckConverter : 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();
    }
} 

视图模型命令代码:

public ICommand SelectUnSelect
    {
        get { return new RelayCommand<object>(parm => this.SelectAndUnSelect(parm));}
    }

如果我在SelectAndUnSelect方法中放置一个调试器,它会显示parm中的object[2],但它们都为null。

观察:如果我将我的命令参数绑定到任何一个绑定上,它就能正常工作。

我在这里错过了什么?

  • Shankar
1个回答

5

我以前也遇到过同样的问题,如果我没记错的话,返回 values.ToList() 而不是只返回 values 应该可以解决这个问题。

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

我也觉得可以。我很想知道为什么只有这种方式可行,而返回对象数组则不行。 - csteinmueller

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接