Search code examples
c#wpfvalidationmultidatatrigger

Enable button when textbox is validated in WPF


I'm trying to disable/enable a button when a textbox is validated.

I get the validation to work (I know that it works because it changes design). But I can't seem to get the button to change from disabled to enabled when the textbox is validate.

Here's my code:

<AdornerDecorator>
    <TextBox Width="150"
             Validation.ErrorTemplate="{StaticResource ValidationErrorTemplate}"
             x:Name="OrgNoTextBox">
        <TextBox.Text>
            <Binding Path="Customer.OrgNo" UpdateSourceTrigger="PropertyChanged" >
                <Binding.ValidationRules>
                    <client:RegexValidationRule ValidatesOnTargetUpdated="True" Pattern="OrgNo" />
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>
</AdornerDecorator>

...

<Button Width="80"
        Height="25" 
        Margin="0 0 5 0" 
        Command="{Binding OkCommand}" 
        HorizontalAlignment="Left" 
        IsDefault="True" 
        Content="Save">
    <Button.Style>
        <Style TargetType="{x:Type Button}">
            <Setter Property="IsEnabled" Value="false" />
            <Style.Triggers>
                <MultiDataTrigger>
                    <MultiDataTrigger.Conditions>
                        <Condition Binding="{Binding  ElementName=OrgNo, Path=(Validation.HasError)}" 
                                       Value="false" />
                    </MultiDataTrigger.Conditions>
                    <Setter Property="IsEnabled" 
                            Value="true" />
                </MultiDataTrigger>
            </Style.Triggers>
        </Style>
    </Button.Style>
</Button>

Solution

  • Your ElementName in the MultiDataTrigger is wrong. It should be OrgNoTextBox instead of OrgNo.