title: WPF Nested ListBox Databinding using ViewModel
body: <p>This is a long one . I am adding code so that you can see what I am trying to do. Let me know if anything is not clear</p>
  
  <p>I am trying to get selected items from nested listbox in multiselct mode . Here is code ( removed lot of unwanted stuff)</p>
  
  <pre><code>public class Item
  {
      public string Name { get; set; }
  
      public IList&lt;Item&gt; SubItems { get; set; } // 
  
      public bool IsSelected { get; set; }
  }
  //Chicken Fried Chicken 
  //A hearty boneless chicken breast, lightly breaded in our special seasonings and 
  //golden fried. Served with garlic mashed potatoes, country gravy and seasonal vegetables
  // from Applebees
  
  //Item - Chicken Fried Chicken 
  //SubItem- mashed potatoes
  //SubItem- country gravy
  //SubItem- seasonal vegetables
  //SubItem- Fries
  //SubItem- Sauted vegetables
  //SubItem- House Salad
  
  public class ItemViewModel : INotifyPropertyChanged, IItemViewModel
  {
  
  
      ObservableCollection&lt;Item&gt; selectedData = new ObservableCollection&lt;Item&gt;();
  
      private ObservableCollection&lt;Item&gt; todaysItems;
      public ObservableCollection&lt;Item&gt; TodaysItems
      {
          get { return todaysItems; }
          private set
          {
              if (todaysItems != value)
              {
                  todaysItems = value;
                  PropertyChanged(this, new PropertyChangedEventArgs("todaysItems"));
              }
          }
      }
  
  
  
      public ItemViewModel(IItemView itemView)
      {
          this.View = itemView;
          this.View.Model = this;
  
          List&lt;Item&gt; items = service.GetAllTestItems();
          TodaysItems = new ObservableCollection&lt;Item&gt;(items);
  
          selectedData.CollectionChanged += (sender, e) =&gt; UpdateSummary();
      }
  
  
  
      #region INotifyPropertyChanged Members
  
      public event PropertyChangedEventHandler PropertyChanged = delegate { };
      private void NotifyPropertyChanged(string propertyName)
      {
          var handler = this.PropertyChanged;
          if (handler != null)
          {
              handler(this, new PropertyChangedEventArgs(propertyName));
          }
      }
      #endregion
  
  
      // How to get Selected Items from ListBox
  
      public ObservableCollection&lt;Item&gt; SelectedData
      {
          get { return selectedData; }
          set
          {
              selectedData = value;
          }
      }
  
  
      private void UpdateSummary()
      {
          // here I can get selected data , I can find which Item is selected and then update its SubItems IsSelected (CLR) Property
          // but something is not right here
      }
  }
  </code></pre>
  
  <p>XAML</p>
  
  <pre><code>&lt;UserControl x:Class="ItemView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:prism="clr-namespace:Microsoft.Practices.Composite.Presentation.Commands;assembly=Microsoft.Practices.Composite.Presentation"
               xmlns:ZCom="clr-namespace:MyProj.Infrastructure;assembly=Infrastructure"&gt;
  
      &lt;Grid  &gt;
          &lt;ListBox ItemsSource="{Binding TodaysItems}"&gt;
              &lt;ListBox.ItemTemplate&gt;
                  &lt;DataTemplate &gt;
                      &lt;Border BorderThickness="1,1,1,1" CornerRadius="2,2,2,2" BorderBrush="Black"&gt;
                          &lt;Grid   MinHeight="50" Width="150" Height="Auto" Margin="0,0,0,0"&gt;
                              &lt;Grid.RowDefinitions&gt;
                                  &lt;RowDefinition /&gt;
                                  &lt;RowDefinition/&gt;
                                  &lt;RowDefinition /&gt;
                              &lt;/Grid.RowDefinitions&gt;
                              &lt;Grid.ColumnDefinitions&gt;
                                  &lt;ColumnDefinition Width="150"/&gt;
                                  &lt;ColumnDefinition Width="0"/&gt;
                              &lt;/Grid.ColumnDefinitions&gt;
                              &lt;TextBlock Margin="4,4,2,2" Grid.Row="0"  Width="Auto" TextWrapping="Wrap" Text="{Binding Path=Name}"  /&gt;
                              &lt;Grid Grid.Row="2" Grid.Column="0" Grid.ColumnSpan="2" &gt;
                                  &lt;Grid.Style&gt;
                                      &lt;Style&gt;
                                          &lt;Style.Triggers&gt;
                                              &lt;DataTrigger Binding="{Binding Path=IsSelected, RelativeSource=
                                                                      {RelativeSource Mode=FindAncestor,AncestorType={x:Type ListBoxItem}}
                                              }" Value="false"&gt;
                                                  &lt;Setter Property="Grid.Visibility" Value="Collapsed"/&gt;
                                              &lt;/DataTrigger&gt;
                                          &lt;/Style.Triggers&gt;
                                      &lt;/Style&gt;
                                  &lt;/Grid.Style&gt;
                                  &lt;Grid.RowDefinitions&gt;
                                      &lt;RowDefinition Height="35"/&gt;
                                      &lt;RowDefinition Height="*"/&gt;
                                  &lt;/Grid.RowDefinitions&gt;
                                  &lt;TextBlock Margin="2,4,2,2"  Grid.Row="0" Width="Auto" FontSize="10" FontStyle="Italic"  TextWrapping="Wrap"  Text="{Binding Path=Note}"/&gt;
  
                                  &lt;ListBox Style="{DynamicResource MyStyle}" Grid.Row="1"  ItemsSource="{Binding Path=Modifiers}"  SelectionMode="Multiple"
                                           ZCom:ListBoxHelper.SelectedItems="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=DataContext.SelectedData}"&gt;
                                      &lt;ListBox.ItemTemplate&gt;
                                          &lt;DataTemplate &gt;
                                              &lt;TextBlock Margin="2,2,2,2"  TextWrapping="Wrap" Text="{Binding Path=Name}"  /&gt;
                                          &lt;/DataTemplate&gt;
  
                                      &lt;/ListBox.ItemTemplate&gt;
                                  &lt;/ListBox&gt;
                              &lt;/Grid&gt;
  
                          &lt;/Grid&gt;
                      &lt;/Border&gt;
                  &lt;/DataTemplate&gt;
              &lt;/ListBox.ItemTemplate&gt;
          &lt;/ListBox&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>I am using ListBoxHelper ( in Infrastructure) from 
  <a href="http://marlongrech.wordpress.com/2009/06/02/sync-multi-select-listbox-with-viewmodel/" rel="nofollow noreferrer">http://marlongrech.wordpress.com/2009/06/02/sync-multi-select-listbox-with-viewmodel/</a></p>
  
  <p>I get the view  with  Item and SubItems. </p>
  
  <p>1) what is better way to  set IsSelected Property of SubItems from nested ListBox </p>
  
  <p>I will add a command which will store selected Item to Database once double clicked. SubItems will be stored as child record  based on its IsSelected value.</p>
  
  <p>2) Is there a way to make SubItems property of  c# class  observable . I would not want to change by adding Observable to the object as it will be in another assembly and may be used by other applications.</p>
  
  <p><strong>Edit 1:</strong>
  Found somewhat helpful question </p>
  
  <blockquote>
    <p><a href="https://stackoverflow.com/questions/1200950/wpf-databinding-to-composite-class-patterns">WPF Databinding to composite class patterns</a></p>
  </blockquote>
  
  <p>But again for this I will have to inherit from INotifyPropertyChanged. </p>
  
  <p><strong>Edit 2:</strong> 
  Let me see if I can explain  better-  ListBox1 is Single select Mode and parent &amp; ListBox 2 is multiselect. ListBox1 is binded (item source) to property which returns observablecollection. ListBox2 is binded to a Property in Item Class (Item.SubItems)  which returns  IList. Item Class has IsSelected Property.I want to be able to select subitems  which should set IsSelected Property  for the subItems to true.   Knowing that there is no INotifyPropertyChanged inheritance in Item Class how can I achieve this. I assume that unless subitems come under  some observable collection any changes will not be notified back to source. Using the selectedData property I can update the subitems by finding parent Item but then to update the  view I will have to firePropertChanged for "items" which involves all items and subitems. I want only subitems change to be notified back by binding mechanism.  Sorry if I am still not clear.</p>
  
  <p>Edit 3:</p>
  
  <p>I Guess there is no way but to implement INotifyPropertyChanged on Item class. Other way would be to implemnt a viewmodel which is very specific to needs of view but this will add up lot of code.</p>