验证 ListBoxItem 而不是 ListBox

Validating a ListBoxItem rather than a ListBox(验证 ListBoxItem 而不是 ListBox)
本文介绍了验证 ListBoxItem 而不是 ListBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 ListBox 绑定到实现 IDataErrorInfo 的对象列表.我已将 ListBox 上的 ValidatesOnDataErrors 属性设置为 True.如果我选择了一个无效的项目,我的 ListBox 周围会出现一个红色边框.如何修改我的 ItemContainerStyle 以使红色边框出现在所选项目而不是实际的 ListBox 周围?

I have a ListBox bound to a list of objects that implement IDataErrorInfo. I have set the ValidatesOnDataErrors property to True on my ListBox. If I select an invalid item, a red border appears around my ListBox. How can I modify my ItemContainerStyle such that the red border appears arround the selected item rather than the actual ListBox?

推荐答案

您可以直接在ListBox的ItemTemplate中设置ValidatesOnDataErrors,在Binding上,将绑定项的属性绑定到显示它的控件上.这应该使红色边框出现在相关控件周围.如果除此之外,边框仍然显示在整个 ListBox 上,您可以在 ListBox 上设置 Validation.ErrorTemplate="{x:Null}".

You can set ValidatesOnDataErrors in the ListBox's ItemTemplate directly, on the Binding which binds the bound item's property to the control that displays it. That should make the red border show up around the control in question. If, in addition to that, the border still shows up on the ListBox as a whole, you can set Validation.ErrorTemplate="{x:Null}" on the ListBox.

如果您不希望边框围绕 ItemTemplate 中的一个控件显示,而是围绕整个 Item,您可以尝试设置 Listboxes ItemBindingGroup.

If you don't want the border to show around one control in the ItemTemplate, but around the whole Item, you could try setting the Listboxes ItemBindingGroup.

这篇关于验证 ListBoxItem 而不是 ListBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)