MultiDataTrigger with multiple ComboBoxes WPF

472 views Asked by At

I would like to enable button only when all specified ComboBoxes have values. However, as soon as I add a second condition, button is enabled from the start

Here is my code

<Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding ElementName=SensorPartNumberComboBox, Path=SelectedValue}" Value="{x:Null}"/>
                        <Condition Binding="{Binding ElementName=SensorTypeComboBox, Path=SelectedValue}" Value="{x:Null}"/>
                        <Condition Binding="{Binding ElementName=SensorBrandNameComboBox, Path=SelectedValue}" Value="{x:Null}"/>
                        <Condition Binding="{Binding ElementName=DimmingProtocolComboBox, Path=SelectedValue}" Value="{x:Null}"/>
                        <Condition Binding="{Binding ElementName=Wired_WirelessComboBox, Path=SelectedValue}" Value="{x:Null}"/>
                    </MultiDataTrigger.Conditions>
                    <Setter Property="IsEnabled" Value="False"/>
                </MultiDataTrigger>
 </Style.Triggers>

I would appreciate any help!

1

There are 1 answers

0
Peter Duniho On BEST ANSWER

MultiDataTrigger requires all of the conditions to be true for it to take effect. I.e. it's equivalent to a logical AND.

In your example, if any value is non-null, the trigger won't take effect and the button will remain enabled.

For a logical OR, instead of using MultiDataTrigger, just use multiple DataTrigger. If any condition of any trigger is true, then that trigger will take effect, with precedence over the default setter for the property in the style.

For example:

<Setter Property="IsEnabled" Value="True"/>
<Style.Triggers>
    <DataTrigger Binding="{Binding ElementName=SensorPartNumberComboBox, Path=SelectedValue}" Value="{x:Null}">
        <Setter Property="IsEnabled" Value="False"/>
    <DataTrigger/>
    <DataTrigger Binding="{Binding ElementName=SensorTypeComboBox, Path=SelectedValue}" Value="{x:Null}">
        <Setter Property="IsEnabled" Value="False"/>
    <DataTrigger/>
    <DataTrigger Binding="{Binding ElementName=SensorBrandNameComboBox, Path=SelectedValue}" Value="{x:Null}">
        <Setter Property="IsEnabled" Value="False"/>
    <DataTrigger/>
    <DataTrigger Binding="{Binding ElementName=DimmingProtocolComboBox, Path=SelectedValue}" Value="{x:Null}">
        <Setter Property="IsEnabled" Value="False"/>
    <DataTrigger/>
    <DataTrigger Binding="{Binding ElementName=Wired_WirelessComboBox, Path=SelectedValue}" Value="{x:Null}">
        <Setter Property="IsEnabled" Value="False"/>
    <DataTrigger/>
 </Style.Triggers>

Alternatively, you might consider putting the logic in your view model, with a single bool property you bind to, and which is set according to the bound SelectedValue properties for the various ComboBox controls.

Yet another alternative would be to use MultiBinding to bind the five view model properties that are bound to the ComboBox.SelectedValue properties, with a IMultiValueConverter that implements the logic.

Of course, these last two options work only if you've got a proper view model with binding set up in the first place (something I strongly encourage, if you haven't already).