WPF DataGrid 验证错误未清除 [英] WPF DataGrid validation errors not clearing
问题描述
所以我有一个 WPF DataGrid
,它绑定到一个 ObservableCollection
.该集合通过 IDataErrorInfo
对其成员进行了验证.如果我以某种方式编辑单元格以使其无效,然后在按 Enter 键之前将其移开,然后返回并使其有效,则该单元格将停止显示无效,但是,!"行首仍然存在,并且 ToolTip
将引用之前的无效值.
Not using Mode=TwoWay
for DataGridTextColumns
解决了一个版本的问题,但是似乎这问题也可能由于其他原因而突然出现.
(任何对为什么不使用 Mode=TwoWay
有很好解释的人首先解决这个问题可能接近解决这个问题)>
同样的事情发生在我的 DataGridComboBoxColumn
上,所以我试图更深入地挖掘.
问题不在于 Control
中的 Binding
显示 DataGridHeaderBorder
内的 ErrorTemplate
.它正在将其 Visibility
绑定到祖先 DataGridRow
的 Validation.HasError
(正如它应该做的那样)并且该部分正在工作.>
Visibility="{Binding (Validation.HasError),转换器={StaticResource bool2VisibilityConverter},RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}"/>
问题是验证错误一旦解决就不会从 DataGridRow
中清除.在我的问题版本中,DataGridRow
以 0 个错误开始.当我输入一个无效值时,它得到了 1 个错误,到目前为止一切顺利.但是当我解决错误时,它跳到了 3 个错误,所有这些错误都是一样的.
在这里我尝试使用 DataTrigger
解决它,如果 Validation.Errors 将
不是 1.它在第一次迭代中效果很好,但是一旦我第二次清除了错误,它又回来了.它不再有 3 个错误,它有 7 个!经过几次迭代后,它超过了 10.ValidationErrorTemplate
设置为 {x:Null}
.Count
我还尝试通过在 BindingExpressions
上执行 UpdateSource
和 UpdateTarget
手动清除错误,但没有骰子.Validation.ClearInvalid
也没有任何影响.在 Toolkit 中查看源代码并没有让我找到任何地方:)
所以我对此没有任何好的解决方案,但我认为无论如何我都应该发布我的发现..
到目前为止,我唯一的解决方法"是在 DataGridRowHeader
ErrorTemplate
<DataGrid.RowStyle><Style TargetType="DataGridRow"><Setter Property="ValidationErrorTemplate" Value="{x:Null}"/></风格></DataGrid.RowStyle><!-- ... --></DataGrid>
So I have a WPF DataGrid
, which is bound to an ObservableCollection
. The collection has validation on its members, through IDataErrorInfo
. If I edit a cell in a way so as to be invalid, and then tab away from it before hitting enter, then come back and make it valid, the cell will stop showing invalid, however, the "!" at the head of the row will still be there, and the ToolTip
will reference the previous, invalid value.
Not using Mode=TwoWay
for DataGridTextColumns
solves one version of the problem, however it seems that this problem can appear out of nowhere for other reasons as well.
(Anyone who has a good explanation as of why not using Mode=TwoWay
solves this in the first place is probably close to a solution to this problem)
The same thing just happened to me with a DataGridComboBoxColumn
so I tried to dig a little deeper.
The problem isn't the Binding
in the Control
that displays the ErrorTemplate
inside DataGridHeaderBorder
. It is binding its Visibility
to Validation.HasError
for the ancestor DataGridRow
(exactly as it should be doing) and that part is working.
Visibility="{Binding (Validation.HasError),
Converter={StaticResource bool2VisibilityConverter},
RelativeSource={RelativeSource AncestorType={x:Type DataGridRow}}}"/>
The problem is that the validation error isn't cleared from the DataGridRow
once it is resolved. In my version of the problem, the DataGridRow
started out with 0 errors. When I entered an invalid value it got 1 error so, so far so good. But when I resolved the error it jumped up to 3 errors, all of which were the same.
Here I tried to resolve it with a DataTrigger
that set the ValidationErrorTemplate
to {x:Null}
if Validation.Errors.Count
wasn't 1. It worked great for the first iteration but once I cleared the error for the second time it was back. It didn't have 3 errors anymore, it had 7! After a couple of more iterations it was above 10.
I also tried to clear the errors manually by doing UpdateSource
and UpdateTarget
on the BindingExpressions
but no dice. Validation.ClearInvalid
didn't have any effect either. And looking through the source code in the Toolkit didn't get me anywhere :)
So I don't have any good solutions to this but I thought I should post my findings anyway..
My only "workaround" so far is to just hide the ErrorTemplate
in the DataGridRowHeader
<DataGrid ...>
<DataGrid.RowStyle>
<Style TargetType="DataGridRow">
<Setter Property="ValidationErrorTemplate" Value="{x:Null}"/>
</Style>
</DataGrid.RowStyle>
<!-- ... -->
</DataGrid>
这篇关于WPF DataGrid 验证错误未清除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!