Show validation error on multiple rows in DataGrid

1.3k Views Asked by At

In my WPF application I have an ObservableCollection of items. Each of item must have a unique name and the name of the item must starts with a letter. I check data validation errors in base class that implements IDataErrorInfo. The problem is that when user enters the existing name the ellipse and the "!" sign appear only in one row, instead of two, but both of them have validation errors. Here is some code of my DataGrid.

    <DataGrid ItemsSource="{Binding Path=IconManagerModel.ConfigurationIcons, 
                             ValidatesOnDataErrors=True}" x:Name="IconsData">
        <DataGrid.Resources>
            <Style x:Key="errorStyle" TargetType="{x:Type TextBlock}" >
                <Setter Property="Padding" Value="2"/>
                <Style.Triggers>
    //Error style for names which not starts with letter
                    <Trigger Property="Validation.HasError" Value="True">
                        <Setter Property="Background" Value="Red"/>
                        <Setter Property="ToolTip" Value="{Binding RelativeSource=
                                RelativeSource FindAncestor, 
                                AncestorType={x:Type DataGridRow}}, 
                                Path=(Validation.Errors)[0].ErrorContent}"/>
                    </Trigger>
    //Error style for duplicated names
                    <DataTrigger Binding="{Binding IsDuplicated}" Value="True">
                        <Setter Property="Background" Value="Red"/>
                        <Setter Property="ToolTip" Value="Duplicated Name" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
            <Style x:Key="ErrorEditStyle" TargetType="{x:Type TextBox}">
                <Setter Property="Padding" Value="2"/>
                <Style.Triggers>
                    <Trigger Property="Validation.HasError" Value="True">
                        <Setter Property="Background" Value="Red"/>
                        <Setter Property="ToolTip" 
                                Value="{Binding RelativeSource=
                                {RelativeSource FindAncestor,
                                AncestorType={x:Type DataGridRow}}, 
                                Path=(Validation.Errors)[0].ErrorContent}"/>
                    </Trigger>                
                </Style.Triggers>
            </Style>
        </DataGrid.Resources>
        <DataGrid.RowValidationErrorTemplate>
            <ControlTemplate>
//This template applies only for the row that has been edited. 
//Other row with the same IconId keeps default style
                <Grid ToolTip="{Binding RelativeSource={RelativeSource FindAncestor, 
                AncestorType={x:Type DataGridRow}}, 
                Path=(Validation.Errors)[0].ErrorContent}" >
                    <Ellipse StrokeThickness="0" Fill="Red"
                                        Width="{TemplateBinding FontSize}" 
                                        Height="{TemplateBinding FontSize}">
                    </Ellipse>
                    <TextBlock Text="!" FontSize="{TemplateBinding FontSize}" 
                                        FontWeight="Bold" Foreground="White" 
                                        HorizontalAlignment="Center"/>
                </Grid>
            </ControlTemplate>
        </DataGrid.RowValidationErrorTemplate>
        <DataGrid.Columns>
            <DataGridTemplateColumn Header="Icon Name">
            </DataGridTemplateColumn>
            <DataGridTextColumn  ElementStyle="{StaticResource ResourceKey=errorStyle}" 
                EditingElementStyle="{StaticResource ResourceKey=ErrorEditStyle}" 
                            Binding="{Binding IconId, ValidatesOnDataErrors=True,
                            NotifyOnValidationError=True,
                            UpdateSourceTrigger=PropertyChanged}"/>
        </DataGridTemplateColumn>
        </DataGrid.Columns>
    </DataGrid>

Thanks in advance.

0

There are 0 best solutions below