Search code examples
c#visual-studio-2010mvvmmultibinding

Multibinding generates "Cannot set MultiBinding because MultiValueConverter must be specified"


I have a button with binding which works fine, see below:

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

Now I have realized that I need yet another piece of information, so I need to send the value of a check-box as well. I modified the VM like this:

<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>

Below is my multi-converter:

/// <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]);
    }
}

What am I doing wrong. I am getting the following error, (which is pointing to my MultiBinding-tag in XAML):

Cannot set MultiBinding because MultiValueConverter must be specified.

Solution

  • you have to implement 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();
        }
    }
    

    then create the resource in xaml

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

    then it should work

    <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>