Note that there are some explanatory texts on larger screens.

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

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload