验证错误模板未显示数据错误
这有点像 WPF 侦探小说!我的验证表明它没有按预期出现
问题
基本上,我在视图模型上使用 IDataErrorInfo 来提供有关数据输入错误的反馈。我可以看到它被调用并按预期工作,但 WPF 永远不会显示绑定控件的验证错误模板!
我已经尝试过使用内置的默认文本框红色边框错误模板、样式错误模板,和一个硬编码模板。在数据错误情况下,这些都不会显示。
这是我的 xaml 绑定代码:
<TextBox x:Name="txtCaseNumber"
Text="{Binding Path=CaseNumber, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"/>
奇怪的是,错误进入了 Validation.Errors 集合。以下代码会导致我的 IDataErrorInfo 错误消息显示在绑定控件旁边。这让我认为问题出在 xaml 方面,而不是 viewmodel 方面。
<ContentPresenter
Content="{Binding ElementName=txtCaseNumber, Path=(Validation.Errors).CurrentItem}" />
此外, System.Windows.Controls.Validation.GetHasError(txtCaseNumber)
返回 true。
如果这有影响的话,我将在 winforms 中托管 WPF。我在代码或样式中找不到任何对 Validation 或 ErrorTemplate 的引用,因此我很确定这不是样式设计自己的目标。
This is a bit of a WPF whodunnit! My validation temples it not appearing when expected
The Problem
Basically, I am using IDataErrorInfo on my view model to provide feedback on data entry errors. I can see this being called and working as expected, but the validation error template for the bound control is never displayed by WPF!
I have tried both using the built-in default textbox red border error template, a style error template, and a hard coded template. None of these are displayed in a data error situation.
Heres my xaml binding code:
<TextBox x:Name="txtCaseNumber"
Text="{Binding Path=CaseNumber, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"/>
Wierdly, the error gets into the Validation.Errors collection. The follow code results in my IDataErrorInfo error message getting displayed next to the bound control. This leads me to think the problem is on the xaml side, rather the viewmodel side.
<ContentPresenter
Content="{Binding ElementName=txtCaseNumber, Path=(Validation.Errors).CurrentItem}" />
Additionally,System.Windows.Controls.Validation.GetHasError(txtCaseNumber)
returns true.
I am hosting WPF in winforms if that makes a difference. I can't find any references in my code or styles to Validation or ErrorTemplate so I'm pretty sure this isn't a styling own goal.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
破解了它。
ErrorTemplate 附加属性使用装饰层显示通过 IDataErrorInfo 报告的数据验证失败的错误模板。 Adorner 层并不总是可用适用于所有情况下的所有控制。您的控件或其父控件之一需要显式提供一个。
默认的 Window 控件模板包含一个 AdornerDecorator,它为其子控件启用装饰器层。这就是为什么我的另一个窗口很好地显示了我的错误模板,因为这个窗口使用了默认的控件模板
没有显示我的错误模板的窗口使用了为我的窗口提供 ControlTemplate 的样式。此控件模板未提供
,因此没有装饰器层来显示我的错误模板。修复方法就像用
包装我的窗口控件模板一样简单;
。Cracked it.
The ErrorTemplate attached property uses the adorner layer to show error templates on data validation failures reported via IDataErrorInfo. The Adorner layer is not always available for all controls in all situations. Your control or one of its parents needs to explicitly provide one.
The default Window control template contains an AdornerDecorator which enables the adorner layer for its children. This why my other window showed my error templates just fine, because this window used the default control template
The window which didn't show my error template had used a style which provided a ControlTemplate for my window. This control template did not provide an
<AdornerDecorator>
, hence no adorner layer to show my error template.The fix was as simple as wrapping my window control template with
<AdornerDecorator>
.