I have two views of some data: a list view (a ListBox
now, but I've been meaning to switch to ListView
) and a fancy graphical representation on a map. In either view the user can click an object and it will be selected in both views. Multiselect is also possible, so each ViewModel
instance has its own IsSelected
property.
Currently I'm binding ListBoxItem.IsSelected
to ViewModel.IsSelected
, but this only works properly if the ListBox
is NOT virtualizing (see here). Unfortunately, disabling virtualization hurts performance and my app has become too slow.
So I have to enable virtualization again. In order to maintain the ViewModel.IsSelected
property of off-screen items, I noticed that ListBox
and ListView
have a SelectionChanged
event that I can (presumably) use to propagate the selection state from the ListBox/ListView
to the ViewModel
.
My question is, how do I propagate selection state in the reverse direction? The SelectedItems
property of ListBox/ListView
is read-only! Suppose the user clicks an item in the graphical representation, but it is off-screen w.r.t. the list. If I just set ViewModel.IsSelected
then the ListBox/ListView
will be unaware of the new selection, and as a consequence it will fail to deselect that item if the user clicks a different item in the list. I could call ListBox.ScrollIntoView
from the ViewModel
, but there are a couple of problems:
- In my UI it's actually possible to select two items with one click if they are in the same location graphically, although they may be located in completely different locations in the
ListBox/ListView
. - It breaks ViewModel isolation (my ViewModel is totally unaware of WPF and I'd like to keep it that way.)
So, my dear WPF experts, any thoughts?
EDIT: I ended up switching to an Infragistics control and using an ugly and rather slow solution. The point is, I no longer need an answer.
See Question&Answers more detail:os