title: WPF ViewModel Data Binding not working when Changing a ListBox's Data Template via a Trigger
body: <p>I am having an issue when using a Trigger to change a ListBox's Data Template for the selected item.</p>
  
  <p>When an item in the ListBox control is selected, I use a trigger to swap out the Data Template so that I can show additional controls, including a ComboBox. Unfortunately when the Data Template switches to the SelectedArticleDataTemplate, the ComboBox's Data Bindings do not seem to work. I am Binding to an Observable Collection in the ViewModel for the ItemSource, as well as a property for the Selected Item. INotifyPropertyChanged is implemented on all Properties in the VieModel and associated Models.</p>
  
  <p>Here is a snippet of the code:</p>
  
  <pre><code>&lt;DataTemplate x:Key="ArticleDataTemplate"&gt;
      &lt;TextBlock Text="{Binding Model.ArticleName}" FontSize="12" Margin="5" /&gt;
  &lt;/DataTemplate&gt;
  
  
   &lt;DataTemplate x:Key="SelectedArticleDataTemplate"&gt;
       &lt;StackPanel Margin="150,0,0,0"&gt;
           &lt;StackPanel Orientation="Horizontal"&gt;
               &lt;TextBlock Text="Select Scale Group :" Width="150" /&gt;
               &lt;ComboBox Width="200" ItemsSource="{Binding ScaleGroups}" SelectedItem="{Binding SelectedGroup}" Margin="0,0,0,10" /&gt;
           &lt;/StackPanel&gt;
       &lt;/StackPanel&gt;
   &lt;/DataTemplate&gt;
  
  
  &lt;Style TargetType="{x:Type ListBoxItem}" x:Key="ContainerStyle"&gt;
      &lt;Setter Property="ContentTemplate" Value="{StaticResource ArticleDataTemplate}" /&gt;
      &lt;Style.Triggers&gt;
          &lt;Trigger Property="IsSelected" Value="True"&gt;
              &lt;Setter Property="ContentTemplate" Value="{StaticResource SelectedArticleDataTemplate}"/&gt;
          &lt;/Trigger&gt;
      &lt;/Style.Triggers&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>For testing purposes, I added the following code outside of the Data Template and it worked perfectly:</p>
  
  <pre><code>&lt;ComboBox Width="200" Margin="0,0,0,10"
            ItemsSource="{Binding ScaleGroups}" 
            SelectedItem="{Binding SelectedGroup}" /&gt;
  </code></pre>
  
  <p>What am I missing?</p>
  
  <p>Thanks</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>The ListBox has a Data Binding to an ObservableCollection of Articles (Target Weights, Tolerances etc.) in the ViewModel. When a user selects an article, I want to let them choose, via the ComboBox, which Scale Group to transmit the article to. <em>- I hope that makes sense.</em></p>
  
  <pre><code> &lt;ListBox Width ="863" Margin="5" Height="422"
            IsSynchronizedWithCurrentItem="True"   
            ItemsSource="{Binding Articles}" 
            SelectedItem="{Binding SelectedArticle}"  
            ItemContainerStyle="{StaticResource ContainerStyle}"
            Background="#FFEAF0FF" /&gt;
  </code></pre>
  
  <p>In my MainViewModel I have the following properties:</p>
  
  <pre><code>        /// &lt;summary&gt;
          /// List of Scale Groups
          /// &lt;/summary&gt;
          public ObservableCollection&lt;string&gt; ScaleGroups
          {
              get { return scaleGroups; }
              set
              {
                  scaleGroups = value;
                  RaisePropertyChanged("ScaleGroups");
              }
          }
  
          /// &lt;summary&gt;
          /// Selected Scale Group
          /// &lt;/summary&gt;
          public string SelectedGroup
          {
              get { return selectedGroup; }
              set
              {
                  selectedGroup = value;
                  RaisePropertyChanged("SelectedGroup");
              }
          }
  
          /// &lt;summary&gt;
          /// List of Articles
          /// &lt;/summary&gt;
          public ObservableCollection&lt;ArticleViewModel&gt; Articles
          {
              get { return articles; }
              set
              {
                  articles = value;
  
                  ApplyCollectionFilter();
                  RaisePropertyChanged("Articles");
              }
          }
  
          /// &lt;summary&gt;
          /// Selected Article
          /// &lt;/summary&gt;
          public ArticleViewModel SelectedArticle
          {
              get { return selectedArticle; }
              set
              {
                  selectedArticle = value;
                  RaisePropertyChanged("SelectedArticle");
              }
          }
  </code></pre>
  
  <p><strong>ANSWER:</strong></p>
  
  <p>Thanks to Clemens for persisting with me.</p>
  
  <p>The following change made all the difference:</p>
  
  <pre><code>&lt;ComboBox Width="250" 
            ItemsSource="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}, Path=DataContext.ScaleGroups}" 
            SelectedItem="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ListBox}, Path=DataContext.SelectedGroup}" 
            IsSynchronizedWithCurrentItem="True" Margin="0,0,0,10" /&gt;
  </code></pre>