Multibinding生成"无法设置MultiBinding,因为必须指定MultiValueConverter"

时间:2013-10-22 06:09:27

标签: c# visual-studio-2010 mvvm multibinding

我有一个带绑定的按钮,工作正常,见下文:

<Button x:Name="licenceFilterSet" Content="Search" Command="{Binding searchCommand}" CommandParameter="{Binding Path=Text, ElementName=licenseTextBox}" />

现在我已经意识到我还需要另一条信息,所以我也需要发送check-box的值。 我像这样修改了虚拟机:

<Button x:Name="licenceFilterSet" Content="Search" Command="{Binding licenseSearchCommand}">
    <Button.CommandParameter>
        <MultiBinding Converter="{StaticResource searchFilterConverter}">
            <Binding Path="Text" ElementName="licenseTextBox" />
            <Binding Path="IsEnabled" ElementName="regularExpressionCheckBox" />
        </MultiBinding>
    </Button.CommandParameter>
</Button>

以下是我的多转换器:

/// <summary>
/// Converter Used for combining license search textbox and checkbox
/// </summary>
public class SearchFilterConverter : IMultiValueConverter
{
    public object Convert(object[] values)
    {
        return new Tuple<String, bool>((String)values[0], (bool)values[1]);
    }
}

我做错了什么。我收到以下错误,(指向我在XAML中的MultiBinding标记):

Cannot set MultiBinding because MultiValueConverter must be specified.

3 个答案:

答案 0 :(得分:5)

你必须实现IMultiConverter

public class SearchFilterConverter : IMultiValueConverter
{
 public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
 {
    return new Tuple<String, bool>((String)values[0], (bool)values[1]);;
 }
 public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

然后在xaml中创建资源

 <Converter:SearchFilterConverter x:Key="searchFilterConverter" />

然后它应该工作

<Button x:Name="licenceFilterSet" Content="Search" Command="{Binding licenseSearchCommand}">
<Button.CommandParameter>
    <MultiBinding Converter="{StaticResource searchFilterConverter}">
        <Binding Path="Text" ElementName="licenseTextBox" />
        <Binding Path="IsEnabled" ElementName="regularExpressionCheckBox" />
    </MultiBinding>
</Button.CommandParameter>
</Button>

答案 1 :(得分:0)

这不是IMultiValueConverter接口的正确实现。

正确的是:

public class SearchFilterConverter : IMultiValueConverter
{
   public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
   {
      ....
   }

   public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
   {
   }
}

参考here

答案 2 :(得分:0)

我知道这个线程很旧,但是昨天我遇到了同样的问题,所有东西都编写正确,但是WPF仍然拒绝定位转换器。帮助我的是通过以下方式分配转换器:

<MultiBinding Converter="{local:ButtonParametersMultiValueConverter}">

那解决了这个问题。