title: WPF: Binding to ListBoxItem.IsSelected doesn't work for off-screen items
body: <p>In my program I have a set of view-model objects to represent items in a ListBox (multi-select  is allowed). The viewmodel has an IsSelected property that I would like to bind to the ListBox so that selection state is managed in the viewmodel rather than in the listbox itself.</p>
  
  <p>However, apparently the ListBox doesn't maintain bindings for most of the off-screen items, so in general the IsSelected property is not synchronized correctly. Here is some code that demonstrates the problem. First XAML:</p>
  
  <pre><code>&lt;StackPanel&gt;
      &lt;StackPanel Orientation="Horizontal"&gt;
          &lt;TextBlock&gt;Number of selected items: &lt;/TextBlock&gt;
          &lt;TextBlock Text="{Binding NumItemsSelected}"/&gt;
      &lt;/StackPanel&gt;
      &lt;ListBox ItemsSource="{Binding Items}" Height="200" SelectionMode="Extended"&gt;
          &lt;ListBox.ItemContainerStyle&gt;
              &lt;Style TargetType="{x:Type ListBoxItem}"&gt;
                  &lt;Setter Property="IsSelected" Value="{Binding IsSelected}"/&gt;
              &lt;/Style&gt;
          &lt;/ListBox.ItemContainerStyle&gt;
      &lt;/ListBox&gt;
      &lt;Button Name="TestSelectAll" Click="TestSelectAll_Click"&gt;Select all&lt;/Button&gt;
  &lt;/StackPanel&gt;
  </code></pre>
  
  <p>C# Select All handler:</p>
  
  <pre><code>private void TestSelectAll_Click(object sender, RoutedEventArgs e)
  {
      foreach (var item in _dataContext.Items)
          item.IsSelected = true;
  }
  </code></pre>
  
  <p>C# viewmodel:</p>
  
  <pre><code>public class TestItem : NPCHelper
  {
      TestDataContext _c;
      string _text;
      public TestItem(TestDataContext c, string text) { _c = c; _text = text; }
  
      public override string ToString() { return _text; }
  
      bool _isSelected;
      public bool IsSelected
      {
          get { return _isSelected; }
          set {
              _isSelected = value; 
              FirePropertyChanged("IsSelected");
              _c.FirePropertyChanged("NumItemsSelected");
          }
      }
  }
  public class TestDataContext : NPCHelper
  {
      public TestDataContext()
      {
          for (int i = 0; i &lt; 200; i++)
              _items.Add(new TestItem(this, i.ToString()));
      }
      ObservableCollection&lt;TestItem&gt; _items = new ObservableCollection&lt;TestItem&gt;();
      public ObservableCollection&lt;TestItem&gt; Items { get { return _items; } }
  
      public int NumItemsSelected { get { return _items.Where(it =&gt; it.IsSelected).Count(); } }
  }
  public class NPCHelper : INotifyPropertyChanged
  {
      public event PropertyChangedEventHandler PropertyChanged;
      public void FirePropertyChanged(string prop)
      {
          if (PropertyChanged != null)
              PropertyChanged(this, new PropertyChangedEventArgs(prop));
      }
  }
  </code></pre>
  
  <p>Two separate problems can be observed. </p>
  
  <ol>
  <li>If you click the first item and then press Shift+End, all 200 items should be selected; however, the heading reports that only 21 items are selected.</li>
  <li>If you click "Select all" then all items are indeed selected. If you then click an item in the ListBox you would expect the other 199 items to be deselected, but this does not happen. Instead, only the items that are on the screen (and a few others) are deselected. All 199 items will not be deselected unless you first scroll through the list from beginning to end (and even then, oddly enough, it doesn't work if you perform scrolling with the little scroll box).</li>
  </ol>
  
  <p>My questions are:</p>
  
  <ol>
  <li>Can someone explain precisely why this occurs?</li>
  <li>Can I avoid or work around the problem?</li>
  </ol>