8

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.
Dale K
  • 25,246
  • 15
  • 42
  • 71
theAlse
  • 5,577
  • 11
  • 68
  • 110

3 Answers3

9

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>
blindmeis
  • 22,175
  • 7
  • 55
  • 74
  • Huh. My code is already structured like this and builds and runs fine, but this error still shows up in the error list (VS2015). From experimenting a bit, the problem seems to be that my IMultiValueConverter is also a MarkupExtension, and this confuses the parser. – dlf Dec 16 '15 at 18:23
  • @dlf Did you ever figure out a way around this? – claudekennilol May 08 '17 at 19:23
  • @claudekennilol Honestly, I don't remember anymore. I suspect I found a way to accomplish whatever I was after without a MultiBinding. – dlf May 08 '17 at 21:00
  • I also ran into this issue (correct implementation, builds and runs fine, but showing an error in the VS Error List - VS 2017). I found out the root cause was that the converter was declared somewhere else, as well as other static resources, and VS was simply giving the wrong false error. I write "wrong false", because the correct false error should have been "The resource could not be resolved". See that error here: https://stackoverflow.com/q/15224864/3220898 – Arkane Feb 02 '22 at 14:51
1

I know this thread is old, but I've faced the same problem yesterday where everything was written correctly yet the WPF was still refusing to locate the converter. What helped me was assigning the converter in the following manner:

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

That solved the issue.

Tomáš Buchta
  • 366
  • 3
  • 8
0

That is not the correct implementation of the IMultiValueConverter interface.

The correct one is:

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)
   {
   }
}

Reference here.

Alberto
  • 15,626
  • 9
  • 43
  • 56