title: Selecting a new item from a listbox does not de-select the existing selected item from another listbox despite sharing the same selected item property
body: <p><strong>Summary</strong></p>
  
  <p>I'm trying to create an invoice. Each item on the invoice can have a sub collection of items. Implementing it was pretty standard, I created an item template to be used by the listbox and created text blocks to bind the listbox's itemsource's properties. Apart from text blocks, this template also houses another listbox containing the collection of sub items.</p>
  
  <p><strong>What I wanted to do:</strong></p>
  
  <p>I wanted to treat this main and sub listbox as one big listbox, at least in terms of the selecteditem. I want to cancel the currently selected row (either from main/sub) whenever I select a new row (still either from main/sub).</p>
  
  <p><strong>What I've tried:</strong></p>
  
  <ol>
  <li><p>First I've tried binding both the main/sub listbox's selected item to a single property but that was a failure. I think it has something to do with the main listbox's selected item getting triggered if I select an item on the sub listbox.</p></li>
  <li><p>My second option was to create two properties with the same type to be used as the main and sub's selected item and placed an instruction to cancel out the each other's value if ever the new selected value is valid. </p></li>
  </ol>
  
  <p><strong>What's currently happening (this is the result of my 2nd try):</strong></p>
  
  <ol>
  <li><p>If I select a sub row it gets selected with no problems</p></li>
  <li><p>If I select another sub row under the same parent as the previously selected row, it also works</p></li>
  <li><p>The problem is that when I select a sub row from another parent after previously selecting a sub row. What happens is that the previous row's selected item status (at least visually) does not get de-selected (it's still highlighted as green as per my is selected trigger) yet the new row gets selected as well and shows the green highlight.</p></li>
  </ol>
  
  <p><strong>The Result</strong></p>
  
  <p><a href="https://gfycat.com/fearlesssnivelinganemone" rel="nofollow noreferrer">https://gfycat.com/fearlesssnivelinganemone</a></p>
  
  <p><strong>Code:</strong></p>
  
  <p>ShopModel</p>
  
  <pre><code>public class ShopModel
  {
      public class Sale : INotifyPropertyChangedClass
      {
          public Sale()
          {
              SaleItemMain = new ObservableCollection&lt;SaleItem&gt;();
          }
  
          private int id;
          public int Id
          {
              get { return id; }
              set { id = value; RaisePropertyChanged("Id"); }
          }
  
          private ObservableCollection&lt;SaleItem&gt; saleItemMain;
          public ObservableCollection&lt;SaleItem&gt; SaleItemMain
          {
              get { return saleItemMain; }
              set { saleItemMain = value; RaisePropertyChanged("SaleItemMain"); }
          }
      }
  
      public class SaleItem : INotifyPropertyChangedClass
      {
          private int id;
          private string name;
          private decimal unitPrice;
          private decimal quantity;
          private ObservableCollection&lt;SaleItem&gt; saleItemSub;
  
          public int Id
          {
              get { return id; }
              set { id = value; RaisePropertyChanged("Id"); }
          }
  
          public string Name
          {
              get { return name; }
              set { name = value; RaisePropertyChanged("Name"); }
          }
  
          public decimal Quantity
          {
              get { return quantity; }
              set { quantity = value;
                    RaisePropertyChanged("Quantity");
                    RaisePropertyChanged("UnitPriceAmount");
                  }
          }
  
          public decimal UnitPrice
          {
              get { return unitPrice; }
              set { unitPrice = value;
                  RaisePropertyChanged("UnitPrice");
                  RaisePropertyChanged("UnitPriceAmount");
              }
          }
  
          public decimal UnitPriceAmount
          {
              get { return Quantity * UnitPrice; }
          }
  
          public ObservableCollection&lt;SaleItem&gt; SaleItemSub
          {
              get { return saleItemSub; }
              set { saleItemSub = value; RaisePropertyChanged("SaleItemSub"); }
          }
      }
  }
  
  public class INotifyPropertyChangedClass : INotifyPropertyChanged
  {
      public event PropertyChangedEventHandler PropertyChanged;
      public void RaisePropertyChanged(string propertyName)
      {
          PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
      }
  }
  </code></pre>
  
  <p>ShopViewModel</p>
  
  <pre><code>public class ShopViewModel : INotifyPropertyChangedClass
  {
      public ShopViewModel()
      {
          Sale = new Sale();
  
          SaleItem mySaleItemSub1 = new SaleItem()
          {
              Id = 1,
              Name = "Sub 1",
              UnitPrice = 200,
              Quantity = 5
          };
  
          SaleItem mySaleItemSub2 = new SaleItem()
          {
              Id = 1,
              Name = "Sub 2",
              UnitPrice = 200,
              Quantity = 5
          };
  
          SaleItem mySaleItemSub3 = new SaleItem()
          {
              Id = 1,
              Name = "Sub 3",
              UnitPrice = 200,
              Quantity = 5
          };
  
          SaleItem mySaleItemSub4 = new SaleItem()
          {
              Id = 1,
              Name = "Sub 4",
              UnitPrice = 200,
              Quantity = 5
          };
  
          SaleItem mySaleItemMain1 = new SaleItem()
          {
              Id = 1,
              Name = "Main 1",
              UnitPrice = 200,
              Quantity = 5,
              SaleItemSub = new ObservableCollection&lt;SaleItem&gt;()
          };
  
          SaleItem mySaleItemMain2 = new SaleItem()
          {
              Id = 1,
              Name = "Main 2",
              UnitPrice = 200,
              Quantity = 5,
              SaleItemSub = new ObservableCollection&lt;SaleItem&gt;()
          };
  
          SaleItem mySaleItemMain3 = new SaleItem()
          {
              Id = 1,
              Name = "Main 3",
              UnitPrice = 200,
              Quantity = 5,
              SaleItemSub = new ObservableCollection&lt;SaleItem&gt;()
          };
  
          mySaleItemMain1.SaleItemSub.Add(mySaleItemSub1);
          mySaleItemMain1.SaleItemSub.Add(mySaleItemSub2);
          mySaleItemMain1.SaleItemSub.Add(mySaleItemSub3);
          mySaleItemMain2.SaleItemSub.Add(mySaleItemSub4);
          Sale.SaleItemMain.Add(mySaleItemMain1);
          Sale.SaleItemMain.Add(mySaleItemMain2);
          Sale.SaleItemMain.Add(mySaleItemMain3);
      }
  
      private Sale sale;
      public Sale Sale
      {
          get { return sale; }
          set { sale = value; RaisePropertyChanged("Sale"); }
      }
  
      private SaleItem selectedSaleItemMain;
      public SaleItem SelectedSaleItemMain
      {
          get { return selectedSaleItemMain; }
          set { selectedSaleItemMain = value;
                  RaisePropertyChanged("SelectedSaleItemMain");
              if (SelectedSaleItemMain != null)
              {
                  SelectedSaleItemSub = null;
                  RaisePropertyChanged("SelectedSaleItemSub");
              }
          }
      }
  
      private SaleItem selectedSaleItemSub;
      public SaleItem SelectedSaleItemSub
      {
          get { return selectedSaleItemSub; }
          set { selectedSaleItemSub = value;
              RaisePropertyChanged("SelectedSaleItemSub");
              if (SelectedSaleItemSub != null)
              {
                  SelectedSaleItemMain = null;
                  RaisePropertyChanged("SelectedSaleItemMain");
              }
          }
      }
  }
  </code></pre>
  
  <p>ShopView</p>
  
  <pre><code>public partial class ShopView : UserControl
  {
      public ShopView()
      {
          DataContext = new ShopViewModel();
          InitializeComponent();
      }
  }
  </code></pre>
  
  <p>Converter (My naming scheme goes as this: <code>(property result)_(value sent)_(summary of what happens)</code></p>
  
  <pre><code>public class Visibility_SaleItemCollection_IfCountGreaterThan0VisibleElseCollapsed : IValueConverter
  {
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
      {
          ObservableCollection&lt;SaleItem&gt; saleItemCollection = value as ObservableCollection&lt;SaleItem&gt;;
          if (saleItemCollection != null)
              if (saleItemCollection.Count &gt; 0)
                  return Visibility.Visible;
          return Visibility.Collapsed;
      }
  
      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  
  <p>XAML</p>
  
  <pre><code>&lt;ListBox 
      SelectedItem="{Binding SelectedSaleItemMain,UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
      ItemsSource="{Binding Sale.SaleItemMain,UpdateSourceTrigger=PropertyChanged}" 
      ItemContainerStyle="{DynamicResource InvoiceListBoxItemMain}" 
      VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" 
      BorderThickness="0" Grid.Row="3"/&gt;
  </code></pre>
  
  <p>(ListBox's ListBoxItem Style):</p>
  
  <pre><code>&lt;Style x:Key="InvoiceListBoxItemMain" TargetType="ListBoxItem"&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type ListBoxItem}"&gt;
                  &lt;Border x:Name="backgroundBorder" Width="Auto" Background="White" &gt;
                      &lt;ContentPresenter Content="{TemplateBinding Content}"&gt;
                          &lt;ContentPresenter.ContentTemplate&gt;
                              &lt;DataTemplate&gt;
                                  &lt;Grid&gt;
                                      &lt;Grid.ColumnDefinitions&gt;
                                          &lt;ColumnDefinition Width="*"/&gt;
                                          &lt;ColumnDefinition Width="60" MaxWidth="60"/&gt;
                                          &lt;ColumnDefinition Width="70" MaxWidth="70"/&gt;
                                          &lt;ColumnDefinition Width="80" MaxWidth="80"/&gt;
                                          &lt;ColumnDefinition Width="50" MaxWidth="50"/&gt;
                                      &lt;/Grid.ColumnDefinitions&gt;
                                      &lt;Grid.RowDefinitions&gt;
                                          &lt;RowDefinition Height="1*" MinHeight="36"/&gt;
                                          &lt;RowDefinition Height="Auto"/&gt;
                                          &lt;RowDefinition Height="1"/&gt;
                                      &lt;/Grid.RowDefinitions&gt;
                                      &lt;TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" Margin="20,0,0,0" Foreground="{DynamicResource BlueGray.Dark.B}" Height="15"/&gt;
                                      &lt;TextBlock Grid.Column="1" HorizontalAlignment="Right" TextWrapping="Wrap" Text="{Binding Quantity, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" Foreground="{DynamicResource BlueGray.Dark.B}"/&gt;
                                      &lt;TextBlock Grid.Column="2" HorizontalAlignment="Right" TextWrapping="Wrap" Text="{Binding UnitPrice, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" Foreground="{DynamicResource BlueGray.Dark.B}"/&gt;
                                      &lt;TextBlock Grid.Column="3" HorizontalAlignment="Right" TextWrapping="Wrap" Text="{Binding UnitPriceAmount, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" Foreground="{DynamicResource BlueGray.Dark.B}"/&gt;
                                      &lt;Button local:Extensions.PathData="{DynamicResource TrashCan.1}" local:Extensions.PathWidth="8" Grid.Column="4" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center" Width="22" Style="{DynamicResource StandardButtonStyle}" Foreground="{DynamicResource Red.Default.B}" Height="22" FontSize="10"/&gt;
                                      &lt;ListBox Visibility="{Binding Path=ItemSource,RelativeSource={RelativeSource AncestorType=ListBox},Converter={StaticResource Visibility_SaleItemCollection_IfCountGreaterThan0VisibleElseCollapsed}}" 
                                               SelectedItem="{Binding DataContext.SelectedSaleItemSub,RelativeSource={RelativeSource AncestorType=ListBox},UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
                                               BorderThickness="0" Grid.ColumnSpan="5" Grid.Row="1" 
                                               ItemContainerStyle="{DynamicResource InvoiceListBoxItemSub}" 
                                               ItemsSource="{Binding SaleItemSub,UpdateSourceTrigger=PropertyChanged}" 
                                               VerticalContentAlignment="Stretch" HorizontalContentAlignment="Stretch" Margin="-1,0" /&gt;
                                      &lt;Rectangle Fill="{DynamicResource Gray.Default.B}" Grid.ColumnSpan="5" Grid.Row="2"/&gt;
                                  &lt;/Grid&gt;
                              &lt;/DataTemplate&gt;
                          &lt;/ContentPresenter.ContentTemplate&gt;
                      &lt;/ContentPresenter&gt;
                  &lt;/Border&gt;
                  &lt;ControlTemplate.Triggers&gt;
                      &lt;Trigger Property="IsSelected" Value="True"&gt;
                          &lt;Setter TargetName="backgroundBorder" Property="Background" Value="{DynamicResource Green.Light.B}"/&gt;
                      &lt;/Trigger&gt;
                  &lt;/ControlTemplate.Triggers&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  
  &lt;Style x:Key="InvoiceListBoxItemSub" TargetType="ListBoxItem"&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type ListBoxItem}"&gt;
                  &lt;Border x:Name="backgroundBorder" Width="Auto" Background="{DynamicResource LightGray.Default.B}"&gt;
                      &lt;ContentPresenter Content="{TemplateBinding Content}"&gt;
                          &lt;ContentPresenter.ContentTemplate&gt;
                              &lt;DataTemplate&gt;
                                  &lt;Grid&gt;
                                      &lt;Grid.ColumnDefinitions&gt;
                                          &lt;ColumnDefinition Width="2*"/&gt;
                                          &lt;ColumnDefinition Width="60" MaxWidth="60"/&gt;
                                          &lt;ColumnDefinition Width="70" MaxWidth="70"/&gt;
                                          &lt;ColumnDefinition Width="80" MaxWidth="80"/&gt;
                                          &lt;ColumnDefinition Width="50" MaxWidth="50"/&gt;
                                      &lt;/Grid.ColumnDefinitions&gt;
                                      &lt;Grid.RowDefinitions&gt;
                                          &lt;RowDefinition Height="Auto" MinHeight="36"/&gt;
                                      &lt;/Grid.RowDefinitions&gt;
                                      &lt;TextBlock HorizontalAlignment="Left" TextWrapping="Wrap" Text="{Binding Name, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" Margin="35,0,0,0" Foreground="{DynamicResource BlueGray.Dark.B}" Height="15"/&gt;
                                      &lt;TextBlock Grid.Column="1" HorizontalAlignment="Right" TextWrapping="Wrap" Text="{Binding Quantity, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" Foreground="{DynamicResource BlueGray.Dark.B}"/&gt;
                                      &lt;TextBlock Grid.Column="2" HorizontalAlignment="Right" TextWrapping="Wrap" Text="{Binding UnitPrice, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" Foreground="{DynamicResource BlueGray.Dark.B}"/&gt;
                                      &lt;TextBlock Grid.Column="3" HorizontalAlignment="Right" TextWrapping="Wrap" Text="{Binding UnitPriceAmount, UpdateSourceTrigger=PropertyChanged}" VerticalAlignment="Center" Foreground="{DynamicResource BlueGray.Dark.B}"/&gt;
                                      &lt;Button local:Extensions.PathData="{DynamicResource TrashCan.1}" local:Extensions.PathWidth="8" Grid.Column="4" HorizontalAlignment="Center" Margin="0" VerticalAlignment="Center" Width="22" Style="{DynamicResource StandardButtonStyle}" Foreground="{DynamicResource Red.Default.B}" Height="22" FontSize="10"/&gt;
                                  &lt;/Grid&gt;
                              &lt;/DataTemplate&gt;
                          &lt;/ContentPresenter.ContentTemplate&gt;
                      &lt;/ContentPresenter&gt;
                  &lt;/Border&gt;
                  &lt;ControlTemplate.Triggers&gt;
                      &lt;Trigger Property="IsSelected" Value="True"&gt;
                          &lt;Setter TargetName="backgroundBorder" Property="Background" Value="{DynamicResource Green.Light.B}"/&gt;
                      &lt;/Trigger&gt;
                  &lt;/ControlTemplate.Triggers&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>