.net – WPF ListBox + Binding + IDataErrorInfo =

data-bindinglistboxnetvalidationwpf

I have a WPF MVVM application. In the View I have a multiselect ListBox. In the ViewModel I have a property for the selected items in the List.

Using a technique similar to the one in the answer to this question I can bind my property to the ListBox.

But I also want to do data validation via IDataErrorInfo. All I want to do is check that the user has selected at least one item in the list. Adding ValidatesOnDataErrors=True to the binding doesn't work.

Is there any way to have a multiselect listbox that's databound with IDataErrorInfo?

Best Answer

If you're using a behavior for binding ViewModel's List with ListBox's selected items, manually update the binding after add/remove item(s):

var binding = BindingOperations.GetBindingExpression(this, SelectedItemsListBoxBehavior.SelectedItemsProperty);
if (binding != null)binding.UpdateSource();