UI 虚拟列表框内的 WPF 验证
我需要在我正在开发的 WPF 应用程序中进行某种数据验证
据我所知,当您将 ValidatesOnDataErrors=True 添加到绑定时,每次解析绑定时(可以是每次)值发生变化,可能是失去焦点时发生的),Binding引擎检查绑定源,通过IDataErrorInfo访问接口,判断是否有错误。
听起来不错,但只有一点需要注意。为了验证数据,需要对其进行渲染。如果您碰巧将该实体的 ObservableCollection
绑定到带有 VirtualizingStackPanel.IsVirtualizing=True
的 ListBox
,则仅验证渲染的项目。由于大多数项目都在视口之外,因此它们不会被渲染。
如何强制验证每个项目?删除 UI 虚拟化不是一个选项。
I need to do some kind of data validation in a WPF application I am developing
As far as I know, when you add ValidatesOnDataErrors=True
to the Binding, everytime the Binding is resolved (it can be every time teh value changes, it can be when focus is lost), Binding engine checks the binding source, through IDataErrorInfo
, access the interface, finds out if there is some error or not.
It sounds good, with only one caveat. In order for data to be validated, it needs to be rendered. If you happen to have an ObservableCollection
of that entity bound to a ListBox
with VirtualizingStackPanel.IsVirtualizing=True
, only rendered items are validated. Since most items are outside viewport, they are not rendered.
How can I force every item to be validated? Removing UI virtualization is not an option.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我找到了一种方法来做到这一点。太丑了。但它有效。假设UI虚拟化列表是 myList
这样,我就得到了所有项目的列表。我将列表滚动到该列表中的每个项目。我更新布局,因为我认为 ScrollIntoView 是异步的。之后,每个项目都已渲染,每个绑定都已执行,并且每个验证都已评估。
还有更好的出路吗?
I have found one way to do so. It's ugly. But it works. Assuming the UI virtualized list is myList
That way, I get a list of all items. I scroll the list to every item in that list. And I update layout, because ScrollIntoView is asynchronous, I think. After that, every item has been rendered, every binding has been executed and every validation has been evaluated.
Are there better ways out there?