title: Why does this simple databinding scenario not work? (ComboBox related)
body: <p>I've been scratching my head on this one for a while now and am stumped at the moment.</p>
  
  <p>The problem scenario is easier to explain as code so hopefully it speaks for itself. First of all, I have a silverlight application with the following in the XAML...</p>
  
  <pre><code>&lt;UserControl x:Class="SilverlightApplication2.Page"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  Width="400" Height="300"&gt;
  
  &lt;UserControl.Resources&gt;
      &lt;DataTemplate x:Key="icTemplate"&gt;
          &lt;ComboBox ItemsSource="{Binding StringsChild}" SelectedItem="{Binding SelectedItem}"/&gt;
      &lt;/DataTemplate&gt;
  &lt;/UserControl.Resources&gt;
  
  &lt;Grid x:Name="LayoutRoot" Background="White"&gt;
      &lt;Grid.RowDefinitions&gt;
          &lt;RowDefinition/&gt;
          &lt;RowDefinition/&gt;
      &lt;/Grid.RowDefinitions&gt;
      &lt;ItemsControl x:Name="ic" ItemTemplate="{StaticResource icTemplate}"&gt;
          &lt;ItemsControl.ItemsPanel&gt;
              &lt;ItemsPanelTemplate&gt;
                  &lt;StackPanel Orientation="Vertical"/&gt;
              &lt;/ItemsPanelTemplate&gt;
          &lt;/ItemsControl.ItemsPanel&gt;
      &lt;/ItemsControl&gt;
  
      &lt;Button Click="Save" Grid.Row="1" Content="GO"/&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p></p>
  
  <p>My code-behind looks like this...(all written in a single class file so that it's easy for you to copy it into your own project and compile)</p>
  
  <pre><code>namespace SilverlightApplication2
  {
      public partial class Page : UserControl
      {
          public ObservableCollection&lt;SomeClass&gt; StringsParent { get; set; } 
  
          public Page()
          {   
              InitializeComponent();
              StringsParent = new ObservableCollection&lt;SomeClass&gt;();
              ic.ItemsSource = StringsParent;
          }
  
          private void Save(object sender, RoutedEventArgs e)
          {
              SomeClass c = new SomeClass();
              c.StringsChild.Add("First");
              c.StringsChild.Add("Second");
              c.StringsChild.SetSelectedItem("Second");
              StringsParent.Add(c);
          }
      }
  
      public class SomeClass
      {
          public SelectableObservablecollection&lt;string&gt; StringsChild { get; set; }
  
          public SomeClass()
          {
              StringsChild = new SelectableObservablecollection&lt;string&gt;();
          }    
      }
  
      public class SelectableObservablecollection&lt;T&gt; : ObservableCollection&lt;T&gt;
      {
          public SelectableObservablecollection()
              : base()
          {
  
          }
  
          public void SetSelectedItem&lt;Q&gt;(Q selectedItem)
          {
              foreach (T item in this)
              {
                  if (item.Equals(selectedItem))
                  {
                      SelectedItem = item;
                      return;
                  }
              }
          }
  
          private T _selectedItem;
          public T SelectedItem
          {
              get
              {
                  return _selectedItem;
              }
              set
              {
                  _selectedItem = value;
                  OnPropertyChanged(new PropertyChangedEventArgs("SelectedItem"));
              }
          }
      }
  }
  </code></pre>
  
  <p>So let me explain...
  I set out to write a generic way of creating an ObservableCollection that has a <strong>SelectedItem</strong> property on it so that when I bind the collection to a ComboBox for example, I can Bind the ComboBox's <strong>SelectedItem</strong> property to it. </p>
  
  <p>However, for some reason, it does  not seem to work when the ComboBox is effectively nested via an ItemTemplate. I effectively have a list of lists, a scenario which is simple enough that I'm lost as to what's wrong.</p>
  
  <p>When you run the code you'll see that the templated ComboBox does pick up the correct items, but it's never set to a SelectedItem despite the binding.</p>
  
  <p>I know it's rather long winded, but...any ideas?</p>
  
  <p>Thanks alot</p>