wpfdatatriggermultitriggermultidatatrigger

WPF MutiDataTrigger not firing setter


I'm trying to use MultiDataTriggers to enabled/disable a button based on the value of two text boxes.

The docs state that the conditions in a MultiDataTrigger are logically ANDed together. In the example below if txtFirst.Text is foo and txtSecond.Text is bar I'd like to enable the button. However, the button always stays disabled (IsEnabled=false).

I'm sure I'm missing a trick here, but a thorough search of Google hasn't gotten me anywhere....

<StackPanel>
        <Button IsEnabled="False" Content="OK">
            <Button.Style>
                <Style>
                    <Style.Triggers>
                        <MultiDataTrigger>
                            <MultiDataTrigger.Conditions>
                                <Condition Binding="{Binding ElementName=txtFirst, Path=Text}" Value="foo"/>
                                <Condition Binding="{Binding ElementName=txtSecond, Path=Text}" Value="bar"/>
                            </MultiDataTrigger.Conditions>
                            <Setter Property="Button.IsEnabled" Value="True"/>
                        </MultiDataTrigger>
                    </Style.Triggers>
                </Style>
            </Button.Style>
        </Button>

        <TextBox x:Name="txtFirst"/>
        <TextBox x:Name="txtSecond"/>
    </StackPanel>

Solution

  • Setting a property directly on the object itself overrides any style-triggered settings. Change your button declaration to:

    <Button Content="OK">
        <Button.Style>
            <Style>
                <Setter Property="IsEnabled" Value="False"/>
                <Style.Triggers>
    

    Now that the IsEnabled property is set in the style, the trigger can change it. Don't ask me why. :)