views:

30

answers:

0

I have a dialog with two textboxes and a button. The button's IsEnabled style is set dynamically based on whether the data validation of the two textboxes succeeded.

    <Button Content="Finish" x:Name="btnFinish" Click="btnFinish_Click">
        <Button.Style>
            <Style TargetType="{x:Type Button}">
                <Setter Property="IsEnabled" Value="false" />
                <Style.Triggers>
                    <!-- Require the controls to be valid in order to press OK -->
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding ElementName=textbox1, Path=(Validation.HasError)}" Value="false" />
                            <Condition Binding="{Binding ElementName=textbox2, Path=(Validation.HasError)}" Value="false" />
                        </MultiDataTrigger.Conditions>
                        <Setter Property="IsEnabled" Value="true" />
                    </MultiDataTrigger>
                </Style.Triggers>
            </Style>
        </Button.Style>
    </Button>

The textboxes are defined as follows:

    <TextBox.Text>
        <Binding Path="SomeProperty" Mode="TwoWay" UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
                <validators:MyValidationRule />
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>

This all works as desired. However, I want the button to be initially disabled. I can't set the IsEnabled property to False directly in the XAML since that overrides the custom setter styles I have defined above. Instead, I've tried to set textbox1's contents to an invalid value and call UpdateSource() on the control.

However, this doesn't seem to cause the button to update its IsEnabled style. I'm probably missing some fundamental here, but I've searched high and low for an answer to this but have come up empty-handed.