1

I would like to make a boolean OR with a MultiBinding and IMultiValueConverter. Unfortunatly the values passed to the converter are alway unset.

  • The first trigger "TriggerStyle" works fine. The button is enable/disable according to "KeyPlugged" Value.

  • The multibiding triger "EnableTriggerStyle" does not works. In the class "BoolTestConverter", the values array is well set to 2 objects but

values.Length = 2

values[0]=DependencyProperty.UnsetValue

values[1]=DependencyProperty.UnsetValue

XAML file :

<Window.Resources>
    <local:BoolTestConverter x:Key="BoolTestConverter"></local:BoolTestConverter>
    <Style x:Key="TriggerStyle" TargetType="Button">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Path=KeyPlugged}" Value="false">
                <Setter Property="Button.IsEnabled" Value="False"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
    <Style x:Key="EnableTriggerStyle" TargetType="Button">
        <Style.Triggers>
            <DataTrigger Value="false">
                <DataTrigger.Binding>
                    <MultiBinding Converter="{StaticResource BoolTestConverter}">
                        <Binding Path="KeyPlugged"/>
                        <Binding Path="KeyOpened"/>
                    </MultiBinding>
                </DataTrigger.Binding>
                <Setter Property="Button.IsEnabled" Value="False"/>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

C# File :

class BoolTestConverter : IMultiValueConverter
{
    public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        bool result = false;

        if (values.Length > 1)
        {
            if (values[0] == null || values[0] == DependencyProperty.UnsetValue)
                throw new NotImplementedException();

            result = System.Convert.ToBoolean(values[0]);
            for (int i = 1; i < values.Length; i++)
            {
                if (values[i] == null || values[i] == DependencyProperty.UnsetValue)
                    throw new NotImplementedException();

                result |= System.Convert.ToBoolean(values[i]);
            }
        }
        else
            throw new NotImplementedException();

        return result;
    }

    public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

Thank you.

Yaegaki2
  • 9
  • 4
  • Rather change the `NotImplementedException` to `NotSupportedException`. The `NotImplementedException` means you did 'forgot' to implement it. – Jeroen van Langen Oct 19 '21 at 21:11
  • Are you sure that the `MultiValueConverter.Convert` is only called when both values are bound? I think not.. They probably will be bound sequential. The first time Convert is called, they both are probably Unset. You should not throw an exception. Rather return `return Binding.DoNothing` – Jeroen van Langen Oct 19 '21 at 21:14
  • Thank you. That's right. It's something i missed. The first call of Convert the "values" are unset and "return Binding.DoNothing" fix the problem. – Yaegaki2 Oct 20 '21 at 05:07
  • I'll add it as answer, so it might be usefull to others. – Jeroen van Langen Oct 20 '21 at 06:42

1 Answers1

1

Everytime a property/value is set the multibinding will be triggered. The first time it is triggered, none of the values are bound (Unset). So the converter is triggered multiple times.

You should not throw an exception. Rather return return Binding.DoNothing or else the application will break.

Jeroen van Langen
  • 21,446
  • 3
  • 42
  • 57