WPF ListBox + Binding + IDataErrorInfo =?

I have a MVVM WPF application. In the view, I have multiselect ListBox. In ViewModel, I have a property for selected items in a list.

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

But I also want to do data validation with IDataErrorInfo. All I want to do is check that the user has selected at least one item in the list. Adding ValidatesOnDataErrors=Trueto the binding does not work.

Is there a way to have a multi selector list associated with IDataErrorInfo data?

+3
source share
2 answers

If you use the behavior to bind the ViewModel List with the selected ListBox items, manually update the binding after adding / removing items (s):

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

Even a multi-user ListBox you can bind SelectedItem

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

and then check when this property changes.

PS
For binding I use this MVVM Multiselect Listbox solution

-1
source

Source: https://habr.com/ru/post/1714288/


All Articles