title: WPF ListView's SelectedItems Count Changes When ItemsPanel is Changed
body: <p>There seems to be a problem with the SelectedItems of ListView when changing the ItemsPanel dynamically. I have implemented MVVM on a ListView whose ItemsSource is binded to a collection of Models. The Model has 2 properties, DisplayName(string) and Selected(bool). And the DataContext for the listview contains a ViewMode(bool) property.</p>
  
  <p>The setup is that the IsSelected property of the ListViewItem is binded to the Selected property of the Model and the ListView's ItemsPanel changes when I changed the ViewMode by click a button.</p>
  
  <p>The problem is that when there is a selected item in the ListView and the ViewMode is changed, the ListView's SelectedItems count increases, even if the selected items do not change. </p>
  
  <p>Note: In my setup, there is only 1 item in the ListView but the SelectedItems count increaes everytime I changed the ViewMode. </p>
  
  <p>Here's the xaml part of the application to test the problem. I think you experts can do the ViewModel/Model part.</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication5.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="MainWindow"
          Width="525"
          Height="350"&gt;
      &lt;StackPanel&gt;
          &lt;Button Command="{Binding ChangeViewModeCommand}"
                  Content="Change ViewMode" /&gt;
          &lt;ListView x:Name="list" ItemsSource="{Binding Models}"&gt;
              &lt;ListView.ItemContainerStyle&gt;
                  &lt;Style TargetType="ListViewItem"&gt;
                      &lt;Setter Property="IsSelected" Value="{Binding Path=Selected, Mode=TwoWay}" /&gt;
                      &lt;Setter Property="Content" Value="{Binding DisplayName}" /&gt;
                  &lt;/Style&gt;
              &lt;/ListView.ItemContainerStyle&gt;
              &lt;ListView.Style&gt;
                  &lt;!--  Default ItemsPanel  --&gt;
                  &lt;Style TargetType="ListView"&gt;
                      &lt;Setter Property="ItemsPanel"&gt;
                          &lt;Setter.Value&gt;
                              &lt;ItemsPanelTemplate&gt;
                                  &lt;StackPanel /&gt;
                              &lt;/ItemsPanelTemplate&gt;
                          &lt;/Setter.Value&gt;
                      &lt;/Setter&gt;
  
                      &lt;Style.Triggers&gt;
                          &lt;!--  Change ItemsPanel  --&gt;
                          &lt;DataTrigger Binding="{Binding ViewMode}" Value="true"&gt;
                              &lt;Setter Property="ItemsPanel"&gt;
                                  &lt;Setter.Value&gt;
                                      &lt;ItemsPanelTemplate&gt;
                                          &lt;WrapPanel /&gt;
                                      &lt;/ItemsPanelTemplate&gt;
                                  &lt;/Setter.Value&gt;
                              &lt;/Setter&gt;
                          &lt;/DataTrigger&gt;
                      &lt;/Style.Triggers&gt;
                  &lt;/Style&gt;
              &lt;/ListView.Style&gt;
          &lt;/ListView&gt;
          &lt;TextBlock Text="{Binding Path=SelectedItems.Count, ElementName=list, StringFormat=Selected Items Count:{0}}" /&gt;
      &lt;/StackPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p><strong>Edit</strong><br>
  I'm adding the code for the ViewModel and the Model class. As you can see, it is as simple as it gets.</p>
  
  <pre><code>public class ViewModel : INotifyPropertyChanged
      {
          public event PropertyChangedEventHandler PropertyChanged;
  
          public ObservableCollection&lt;Model&gt; Models { get; private set; }
  
          private bool viewMode;
          public bool ViewMode
          {
              get { return viewMode; }
              set 
              {
                  if (viewMode != value)
                  {
                      viewMode = value;
                      OnPropertyChanged("ViewMode");
                  }
              }
          }
  
          public ICommand ChangeViewModeCommand
          {
              get { return new DelegateCommand(() =&gt; ViewMode = ViewMode ? false : true); }
          }
  
          public ViewModel()
          {
              Models = new ObservableCollection&lt;Model&gt;();
              Models.Add(new Model() { DisplayName = "Model1" });
          }
  
          private void OnPropertyChanged(string propertyName)
          {
              if (PropertyChanged != null)
                  PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          }
  </code></pre>
  
  <p><em>Model Class</em>  </p>
  
  <pre><code>public class Model : INotifyPropertyChanged
  {
      public event PropertyChangedEventHandler PropertyChanged;
  
      private bool isSelected;
      public bool Selected
      {
          get { return isSelected; }
          set 
          { 
              isSelected = value; OnPropertyChanged("Selected"); 
          }
      }
  
      private string display;
      public string DisplayName
      {
          get { return display; }
          set { display = value; OnPropertyChanged("Display"); }
      }
  
  
      private void OnPropertyChanged(string propertyName)
      {
          if (PropertyChanged != null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          }
      }
  }
  </code></pre>
  
  <p>What is weird is that eventhough there is only 1 item in the Models collection, the ListView.SelectedItems.Count increases. </p>
  
  <p>Thanks</p>