WPF数据表格行验证错误在编辑后未清除

8
在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>
1个回答

5

在行提交或取消编辑后,您需要使用空字符串调用 notifyPropertyChanged 来刷新 DataBinding From 对象,这将刷新您的界面。请按以下方式使用:

RaiseNotifyPropertyChanged(""); 

如何实现这个?我能否得到一个完整的使用示例? - Klasik
2
对我没用。如果任何单元格存在错误,DataGridRow上的Validation.ErrorsProperty在我离开“添加新行”编辑时会累积错误。这是一个可怕的机制。 - Hrvoje Batrnek
1
这就是为什么我使用控件进行编辑而不是DataGrid,它非常笨拙!我真的看不出这种行为背后的逻辑,是个bug吗? - Hrvoje Batrnek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接