UI 虚拟列表框内的 WPF 验证

发布于 2024-10-08 06:29:53 字数 405 浏览 3 评论 0原文

我需要在我正在开发的 WPF 应用程序中进行某种数据验证

据我所知,当您将 ValidatesOnDataErrors=True 添加到绑定时,每次解析绑定时(可以是每次)值发生变化,可能是失去焦点时发生的),Binding引擎检查绑定源,通过IDataErrorInfo访问接口,判断是否有错误。

听起来不错,但只有一点需要注意。为了验证数据,需要对其进行渲染。如果您碰巧将该实体的 ObservableCollection 绑定到带有 VirtualizingStackPanel.IsVirtualizing=TrueListBox,则仅验证渲染的项目。由于大多数项目都在视口之外,因此它们不会被渲染。

如何强制验证每个项目?删除 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

謸气贵蔟 2024-10-15 06:29:53

我找到了一种方法来做到这一点。太丑了。但它有效。假设UI虚拟化列表是 myList

for (Int32 i = 0; i < myList.Items.Count; i++)
{
     myList.ScrollIntoView(myList.Items[i]);
}
UpdateLayout();

这样,我就得到了所有项目的列表。我将列表滚动到该列表中的每个项目。我更新布局,因为我认为 ScrollIntoView 是异步的。之后,每个项目都已渲染,每个绑定都已执行,并且每个验证都已评估。

还有更好的出路吗?

I have found one way to do so. It's ugly. But it works. Assuming the UI virtualized list is myList

for (Int32 i = 0; i < myList.Items.Count; i++)
{
     myList.ScrollIntoView(myList.Items[i]);
}
UpdateLayout();

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文