title: Synchronizing multi-select ListBox with MVVM
body: <p>I have two views of some data: a list view (a <code>ListBox</code> now, but I've been meaning to switch to <code>ListView</code>) 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 <code>ViewModel</code> instance has its own <code>IsSelected</code> property.</p>
  
  <p>Currently I'm binding <code>ListBoxItem.IsSelected</code> to <code>ViewModel.IsSelected</code>, but this only works properly if the <code>ListBox</code> is NOT virtualizing (<a href="https://stackoverflow.com/questions/7097999/wpf-binding-to-listboxitem-isselected-doesnt-work-for-off-screen-items">see here</a>). Unfortunately, disabling virtualization hurts performance and my app has become too slow.</p>
  
  <p>So I have to enable virtualization again. In order to maintain the <code>ViewModel.IsSelected</code> property of off-screen items, I noticed that <code>ListBox</code> and <code>ListView</code> have a <code>SelectionChanged</code> event that I can (presumably) use to propagate the selection state from the <code>ListBox/ListView</code> to the <code>ViewModel</code>.</p>
  
  <p>My question is, how do I propagate selection state in the reverse direction? The <code>SelectedItems</code> property of <code>ListBox/ListView</code> 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 <code>ViewModel.IsSelected</code> then the <code>ListBox/ListView</code> will be unaware of the new selection, and as a consequence it will fail to <em>deselect</em> that item if the user clicks a different item in the list. I could call <code>ListBox.ScrollIntoView</code> from the <code>ViewModel</code>, but there are a couple of problems:</p>
  
  <ul>
  <li>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 <code>ListBox/ListView</code>.  </li>
  <li>It breaks ViewModel isolation (my ViewModel is totally unaware of WPF and I'd like to keep it that way.)</li>
  </ul>
  
  <p>So, my dear WPF experts, any thoughts?</p>
  
  <p>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.</p>