title: WPF Listbox SelectionChanged
body: <p>I currently have an <code>Entity</code> that has a collection property on it. I want to know if why would the <code>SelectionChanged</code> only fire once and it won't trigger the <code>SelectionChanged</code> again once I try to select the item that was previously selected. </p>
  
  <p><strong>MainWindowViewModel</strong></p>
  
  <pre><code>  public MainWindowViewModel()
      {
          var a = new List&lt;Test&gt;();
  
          a.Add(new Test() { Name = "Leo", Test1 = new List&lt;Test1&gt; { new Test1() { Content = "aaa"} } });
          a.Add(new Test() { Name = "2", Test1 = new List&lt;Test1&gt; { new Test1() { Content = "bbb"} } });
          a.Add(new Test() { Name = "Le33o", Test1 = new List&lt;Test1&gt; { new Test1() { Content = "ccc"} } });
          A = a;
      }
  
      private List&lt;Test&gt; _a;
      public List&lt;Test&gt; A
      {
          get { return _a; }
          set { _a = value; OnPropertyChanged("A");}
      }
  
      protected void OnPropertyChanged(string name)
      {
          PropertyChangedEventHandler handler = PropertyChanged;
          if (handler != null)
          {
              handler(this, new PropertyChangedEventArgs(name));
          }
      }
      public event PropertyChangedEventHandler PropertyChanged;
  </code></pre>
  
  <p><strong>My Mainwindow</strong></p>
  
  <pre><code>public MainWindow()
          {
              InitializeComponent();
              this.DataContext = new MainWindowViewModel();
          }
          private void Test(object sender, SelectionChangedEventArgs e)
          {
          }
  </code></pre>
  
  <p><strong>My listbox structure</strong> </p>
  
  <pre><code>public class Test
  {
      public List&lt;Test1&gt; Test1 { get; set; }
      public string Name
      {
          get;set;
      }
  }
  
  public class Test1
  {
      public string Content { get; set; }
  }
  </code></pre>
  
  <p>I select the first object, the event fires, I select the second object, the event fires, I select the first object, the event doesn't fire, I select third object, the event fires. It seems like it only triggers and calls the event once. </p>
  
  <p>My <code>XAML</code> Code:</p>
  
  <pre><code>&lt;ItemsControl x:Name="Lists" ItemsSource="{Binding A}" Grid.Row="1"&gt;
          &lt;ItemsControl.ItemTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;StackPanel&gt;
                      &lt;TextBlock Text="{Binding Name}" FontWeight="Bold" 
                                                          Style="{StaticResource DefaultTextBlockStyle}" /&gt;
  
                      &lt;ListBox  SelectionChanged="Test"  ItemsSource="{Binding Test1}"
                                   Margin="5,0,0,0" ScrollViewer.HorizontalScrollBarVisibility="Disabled"&gt;
                          &lt;ListBox.ItemTemplate&gt;
                              &lt;DataTemplate&gt;
                                  &lt;StackPanel Orientation="Horizontal"&gt;
                                      &lt;TextBlock Text="{Binding Content}" /&gt;
                                  &lt;/StackPanel&gt;
                              &lt;/DataTemplate&gt;
                          &lt;/ListBox.ItemTemplate&gt;
                      &lt;/ListBox&gt;
                  &lt;/StackPanel&gt;
              &lt;/DataTemplate&gt;
          &lt;/ItemsControl.ItemTemplate&gt;
      &lt;/ItemsControl&gt;
  </code></pre>
  
  <p>The test method is just an empty method I just want to hit the breakpoint every time I change.</p>
  
  <pre><code>  private void Test(object sender, SelectionChangedEventArgs e)
      {
      }
  </code></pre>
  
  <p><strong>Update 1</strong>: I tried to reproduce this in a simple WPF app, it seems that the ListBoxItem is getting IsEnabled to false but I snooped it and all the controls are enabled. It just getting a grey background that looks like disabled. Will try to investigate further.</p>
  
  <p><strong>Update 2</strong>: It seems that the ListBoxItem IsSelected property is not being unset when you change an item.</p>