在WPF中使用DataGrid时,我正在尝试通过INotifyDataErrorInfo进行错误验证以获得正确的行为。 我有一个实现该接口的类的ObservableCollection,并将该集合绑定到DataGrid。 当存在错误时,单元格将具有红色边框,行将在前面显示红色!默认情况下,一切都很好。 当仍在编辑时,当错误消失时,红色边框和红色!都会消失。 到目前为止,一切正常!然而,当我离开该行(通过键盘Enter/Tab或鼠标),然后再回来并删除错误时,红色单元格边框消失了,但红色!保留。 我知道这个问题以前已经被提出过,例如在这里:WPF DataGrid validation errors not clearing。 但是,那里的解决方案并没有解决这个问题,除了完全隐藏行验证错误。 (与第二个答案here结合使用也可以...)或者我的问题是用户能够离开单元格的编辑模式,即使存在验证错误? 最好,我想限制这一点,并在进一步编辑之前强制解决错误,但我不知道如何在没有大量代码的情况下强制执行此操作...这是XML(RowValidationErrorTemplate来自这里: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>