title: Force Update of RelativeSource Binding in WPF
body: <p>for my application, I have an ItemsControl which has a distinct template (shorthanded as Template2) for the first item, and a default template (Template1) for the rest of the items. I have done so with the following XAML:</p>
  
  <pre><code>&lt;DataTemplate x:Key="MyDataTemplate"&gt;
      &lt;ContentPresenter x:Name="TemplateContentPresenter"
                        Content="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}"
                        ContentTemplate="{StaticResource Template1}"/&gt;
      &lt;DataTemplate.Triggers&gt; 
          &lt;DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, 
       Converter= {StaticResource IsFirstItemInCollectionConverter}}"
                       Value="True"&gt;
              &lt;Setter TargetName="TemplateContentPresenter"
                      Property="ContentTemplate"
                      Value="{StaticResource Template2}"/&gt;
          &lt;/DataTrigger&gt;
      &lt;/DataTemplate.Triggers&gt;
  &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>The IsFirstItemInCollectionConverter's code is as follows:</p>
  
  <pre><code>public class IsFirstItemInCollectionConverter : IValueConverter
  {
      public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
      {
          DependencyObject item = (DependencyObject)value;
          ItemsControl ic = ItemsControl.ItemsControlFromItemContainer(item);
          Console.WriteLine("converted");
          return (ic.ItemContainerGenerator.IndexFromContainer(item) == 0);
      }
  
      public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
      {
          throw new NotImplementedException();
      }
  }
  </code></pre>
  
  <p>The problem is that my binding does not update when I make a change in the itemssource of the itemsControl. (In the view that displays the itemsControl, I have buttons which can add and remove items). The binding updates and shows the correct template only after I switch the view, and reload it. How can I correct this behavior? I'm assuming I have to set the UpdateSourceTrigger to explicit and update the source manually, but I have no idea how to do it inside a datatemplate. Thanks in advance for the help.</p>