3

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?

Community
  • 1
  • 1
Cameron MacFarland
  • 70,676
  • 20
  • 104
  • 133
  • Can you post your code? Define "doesn't work"? Are you getting an error? Is the imlementation of IDataErrorInfo called? – Anderson Imes Aug 05 '09 at 07:10

2 Answers2

0

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

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

Robert Vuković
  • 4,677
  • 6
  • 31
  • 47