title: Binding ComboBox in ListBox
body: <p>I cant figure this one out.  If I just have the combo-box by itself not embedded in a list box it will populate and selected the values I need.  </p>
  
  <p>The XAML</p>
  
  <pre><code>&lt;ComboBox Name="comboBox1" 
            Height="23" 
            DataContext="{Binding Combox}" 
            ItemsSource="{Binding Comboxes}" 
            DisplayMemberPath="PV" 
            SelectedValuePath="PK"                  
            SelectedItem="{Binding SelectedItem}"
            VerticalAlignment="Top" 
            Width="120" /&gt;       
  </code></pre>
  
  <p>The Code Behind</p>
  
  <pre><code>    public MainWindow()
      {
          InitializeComponent();
  
          DataAttribute d = new DataAttribute(2, "blue");
  
          Combox c = new Combox();
  
          c.SelectedItem = d;
          c.Comboxes.Add(new DataAttribute(1, "red"));
  
          c.Comboxes.Add(new DataAttribute(3, "Black"));
          c.Comboxes.Add(c.SelectedItem);
  
          comboBox1.DataContext = c;
      }
  </code></pre>
  
  <p>Class for holding data</p>
  
  <pre><code>public class Combox: INotifyPropertyChanged
  {
      public Combox()
      {
          Comboxes = new List&lt;DataAttribute&gt;();            
      }
  
      private DataAttribute _selectedItem;// = new DataAttribute(-1, "NA");
  
      public List&lt;DataAttribute&gt; Comboxes { get; set; }
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      public DataAttribute SelectedItem
      {
          get { return _selectedItem; }
          set
          {
              if (_selectedItem == value) return;
              _selectedItem = value;
              OnPropertyChanged("SelectedValue");
          }
      }
  
      private void OnPropertyChanged(string propertyName)
      {
          if (PropertyChanged != null)
              PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
      }        
  }
  
  public class DataAttribute
  {
      public DataAttribute() { }
  
      public DataAttribute(int pk, string pv)
      {
          PK = pk;
          PV = pv;
      }
  
      public int PK { get; set; }
      public string PV { get; set; }
  
      public override string ToString()
      {
          return PV;
      }
  }
  </code></pre>
  
  <p>All that works fine, but as soon as I try to create a list of com boxes in the listbox nothing.  I can see the combo but no data.  How on earth to you bind to it is XAML?</p>
  
  <pre><code>    public MainWindow()
      {
          InitializeComponent();
  
          List&lt;Combox&gt; com = new List&lt;Combox&gt;();
          DataAttribute d = new DataAttribute(2, "blue");
  
          Combox c = new Combox();
  
          c.SelectedItem = d;
          c.Comboxes.Add(new DataAttribute(1, "red"));
  
          c.Comboxes.Add(new DataAttribute(3, "Black"));
          c.Comboxes.Add(c.SelectedItem);
  
          com.Add(c);
  
          lstTest.ItemSource = com;
      }
  </code></pre>
  
  <p>As here is the XAML with a listbox.  It no longer binds...</p>
  
  <pre><code>    &lt;ListBox Name="lstTest" ItemsSource="{Binding}"&gt;        
          &lt;ListBox.ItemTemplate&gt;
              &lt;DataTemplate&gt;
                  &lt;ComboBox Name="comboBox1" 
                        DataContext="{Binding Combox}" 
                        ItemsSource="{Binding Comboxes}" 
                        DisplayMemberPath="PV" 
                        SelectedValuePath="PK"                  
                        SelectedItem="{Binding SelectedItem}"
                        VerticalAlignment="Top" 
                        Width="120" /&gt;        
              &lt;/DataTemplate&gt;
          &lt;/ListBox.ItemTemplate&gt;            
      &lt;/ListBox&gt;
  </code></pre>
  
  <p>I am stumped as it was hard enough trying to figure out how to just get the selected object to appear without the list-box...</p>