【问题标题】:Validating a ListBoxItem rather than a ListBox验证 ListBoxItem 而不是 ListBox
【发布时间】:2012-11-07 13:58:50
【问题描述】:
我有一个 ListBox 绑定到实现 IDataErrorInfo 的对象列表。我已将 ListBox 上的 ValidatesOnDataErrors 属性设置为 True。如果我选择了一个无效的项目,我的 ListBox 周围会出现一个红色边框。如何修改我的 ItemContainerStyle 以使红色边框出现在所选项目而不是实际的 ListBox 周围?
【问题讨论】:
标签:
c#
wpf
validation
listbox
【解决方案1】:
您可以直接在 ListBox 的 ItemTemplate 中设置 ValidatesOnDataErrors,在将绑定项的属性绑定到显示它的控件的 Binding 上。这应该使红色边框出现在相关控件周围。如果除此之外,边框仍然显示在整个 ListBox 上,您可以在 ListBox 上设置 Validation.ErrorTemplate="{x:Null}"。
如果您不希望边框围绕 ItemTemplate 中的一个控件显示,而是围绕整个 Item,您可以尝试设置 Listboxes ItemBindingGroup。