编辑后WPF Datagrid行验证错误未清除

时间:2021-11-12 09:01:47

Using a DataGrid in WPF, I am trying to get correct behaviour while using error validation via INotifyDataErrorInfo.

在WPF中使用DataGrid,我试图通过INotifyDataErrorInfo使用错误验证时获得正确的行为。

I have an ObservableCollection of a class that implements that interface, an bind that collection to the DataGrid. When there is an error, the cell will have a red border, and row will have a red ! in front. All default, all good. When still editing, when the error is gone, the red border and red ! will both disappear. So far, so good!

我有一个实现该接口的类的ObservableCollection,一个绑定到DataGrid的集合。出现错误时,单元格将显示红色边框,行将显示红色!前面。全部默认,都很好。当还在编辑时,当错误消失时,红色边框和红色!都会消失。到现在为止还挺好!

However, when I leave the row (via keyboard Enter/Tab or with the mouse), then come back it and then remove the error, the red cell border disappears, but the red ! stays.

但是,当我离开行(通过键盘输入/ Tab或鼠标),然后返回它然后删除错误,红色单元格边框消失,但红色!停留。

I am aware this question has been raised before, for example here: WPF DataGrid validation errors not clearing. However, the solutions there do not resolve this, apart from hiding the row validation error altogether. (Which, in combination with something like the second answer here is also quite ok...)

我知道之前已经提出过这个问题,例如:WPF DataGrid验证错误未清除。但是,除了完全隐藏行验证错误之外,那里的解决方案无法解决此问题。 (其中,结合第二个答案,这里也很好......)

Or is my problem rather that the user is able to leave the editing mode of the cell even though there is a validation error? Preferably, I would like to restrict this, and force the resolution of the error first, before further editing can occur, but I don't know how to enforce this without lots of code...

或者我的问题是,即使存在验证错误,用户也可以离开单元格的编辑模式?最好,我想限制这一点,并在进一步编辑之前先强制解决错误,但我不知道如何在没有大量代码的情况下强制执行此操作...

Here is the XML (the RowValidationErrorTemplate comes from here: link):

这是XML(RowValidationErrorTemplate来自这里:链接):

<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 个解决方案

#1


3  

you need to raise notifyPropertyChanged with empty string after row committed or canceled edit to refresh the DataBinding From object and this will refresh your interface use it like that:

您需要在提交或取消行之后使用空字符串引发notifyPropertyChanged以刷新DataBinding From对象,这将刷新您的界面使用它:

RaiseNotifyPropertyChanged(""); 

#1


3  

you need to raise notifyPropertyChanged with empty string after row committed or canceled edit to refresh the DataBinding From object and this will refresh your interface use it like that:

您需要在提交或取消行之后使用空字符串引发notifyPropertyChanged以刷新DataBinding From对象,这将刷新您的界面使用它:

RaiseNotifyPropertyChanged("");