How to merge single binding validationrule errors to bindinggroup?

Flithor 196 Reputation points
2020-04-07T02:19:02.657+00:00

Example:

<Grid>
    <Grid.BindingGroup>
        <BindingGroup x:Name="Group"/>
    </Grid.BindingGroup>
    <TextBox>
        <TextBox.Text>
            <Binding Path="Name" BindingGroupName="Group">
                <Binding.ValidationRules>
                    <rules:MyCustomCommonRule/>
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>
    <TextBox Text="{Binding Name2}"/>
    <Button Content="Commit"
              IsEnabled="{Binding ElementName=Group,
              Path=HasValidationError,
              Converter={StaticResource NotConverter}}"/>
</Grid>

If MyCustomCommonRule has return validation error, how to make the button disabled?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,784 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Alex Li-MSFT 1,096 Reputation points
    2020-04-07T06:57:40.807+00:00

    Hi,

    Welcome to our Microsoft Q&A platform!

    Grid doesn't have HasValidationError property.

    If you want to use code behind ,see my code:

    if (Validation.GetHasError(GridName) == true)
    {
    ButtonName.IsEnabled = false;
    }
    

    or you can use xaml:

    <Button   >
                <Button.Style>
                    <Style TargetType="Button">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding ElementName=Grid1,Path=(Validation.HasError),UpdateSourceTrigger=PropertyChanged}" Value="True">
                                <Setter Property="IsEnabled" Value="False"/>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Button.Style>
    

    Thanks.

    1 person found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.