3
votes

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?

2
Can you post your code? Define "doesn't work"? Are you getting an error? Is the imlementation of IDataErrorInfo called? - Anderson Imes

2 Answers

0
votes

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();
-1
votes

Even it is multiselect ListBox you can bind SelectedItem

 SelectedItem="{Binding FakeSelectedItem, ValidatesOnDataErrors=True}"

and then do a validation when this property changes.

PS
For the binding I am using this solution MVVM Multiselect Listbox