Using DataGrid in WPF, I am trying to get the correct behavior when using error checking through INotifyDataErrorInfo.
I have an ObservableCollection of a class that implements this interface, binds this collection to a DataGrid. When there is an error, the cell will have a red border, and the line will be red! in front. Everything is by default, everything is fine. When you are still editing, when the error disappears, the red frame and red! both will disappear. So far so good!
However, when I leave the line (using the Enter / Tab keyboard or using the mouse), return it and then delete the error, the red border of the cell will disappear, but red! stays.
I know this question was raised earlier, for example here: WPF DataGrid validation errors not clearing . However, the solutions there do not eliminate this, except to hide the string check error. (Which, combined with something like a second answer , is also fine here ...)
Or is my problem more likely that the user can leave the cell edit mode even if there is a validation error? Preferably, I would like to limit this and first force the error to be resolved before further editing can occur, but I don't know how to ensure this without a lot of code ...
Here is the XML (RowValidationErrorTemplate: link appears here: link ):
<UserControl x:Class="CustomDG" ...etc... mc:Ignorable="d" d:DesignHeight="300" d:DesignWidth="300" d:DataContext="{d:DesignInstance Type=viewmodels:TestViewModel}"> <Grid> <DataGrid ItemsSource="{Binding Path=testCollection}" AutoGenerateColumns="False" RowHeight="18" CanUserResizeRows="False" RowHeaderWidth="18" > <DataGrid.RowValidationErrorTemplate> <ControlTemplate> <Grid Margin="0,-2,0,-2" 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}" /> <TextBlock Text="!" FontSize="{TemplateBinding FontSize}" FontWeight="Bold" Foreground="White" HorizontalAlignment="Center" /> </Grid> </ControlTemplate> </DataGrid.RowValidationErrorTemplate>--> <DataGrid.Columns> <DataGridTextColumn Header="Name" Binding="{Binding Path=Name, ValidatesOnNotifyDataErrors=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" > </DataGridTextColumn> </DataGrid.Columns> </DataGrid> </Grid> </UserControl>