Validating a ListBoxItem rather than a ListBox

2019-06-02 21:52发布

问题:

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?

回答1:

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.

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.