简体   繁体   中英

WPF MultiBinding and IMultiValueConverter makes a boolean operation

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM