title: WPF Changing ContentTemplate in UserControl by Trigger
body: <p><strong>Expectations</strong>  </p>
  
  <p>My control should appear as shown in <a href="http://www.endys.cz/images/tmp/figure1.png" rel="nofollow"><em>Figure 1</em></a>. After clicking on the button titled "Second" should contents of the control change, as shown in <a href="http://www.endys.cz/images/tmp/figure2.png" rel="nofollow"><em>Figure 2</em></a>. In this figure the state should be content again to change back after sending the key Enter the text box.</p>
  
  <p><strong>Solution</strong></p>
  
  <p>I created two DataTemplate. These DataTemplates should change based on changes in the properties IsEditable. Default DataTemplate is called <strong><em>CompactDataTemplate</em></strong> and should be used when the value <strong><em>IsEditable</em></strong> is equal FALSE.
  When the <strong><em>IsEditable</em></strong> turns TRUE, the <strong><em>EditableDataTemplate</em></strong> should be used.</p>
  
  <p>File <strong>TestUserControl.xaml</strong></p>
  
  <pre><code>&lt;UserControl x:Class="JP4.Controls.TestControl"
           xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
           xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
           mc:Ignorable="d" 
           d:DesignHeight="26" d:DesignWidth="200" Style="{DynamicResource TestControlStyle}"&gt;
  &lt;UserControl.Resources&gt;
  </code></pre>
  
  <p><strong><em>CompactDataTemplate</em></strong>  </p>
  
  <pre><code>&lt;DataTemplate x:Key="CompactDataTemplate" DataType="{x:Type UserControl}"&gt;
      &lt;StackPanel Orientation="Horizontal"&gt;
          &lt;Button Content="First Button" /&gt;
          &lt;Button Content="Second Button" Click="SecondButton_Click" /&gt;
      &lt;/StackPanel&gt;
  &lt;/DataTemplate&gt;
  </code></pre>
  
  <p><strong><em>EditableDataTemplate</em></strong>  </p>
  
  <pre><code>&lt;DataTemplate x:Key="EditableDataTemplate" DataType="{x:Type UserControl}"&gt;
      &lt;StackPanel Orientation="Horizontal"&gt;
          &lt;TextBox Text="Press enter" /&gt;
      &lt;/StackPanel&gt;
  &lt;/DataTemplate&gt;
  </code></pre>
  
  <p><strong><em>TestControlStyle</em></strong> contains trigger that should firing in case IsEditable has been changed  </p>
  
  <pre><code>&lt;Style x:Key="TestControlStyle" TargetType="{x:Type UserControl}"&gt;
          &lt;Setter Property="ContentTemplate" Value="{StaticResource CompactDataTemplate}" /&gt;
          &lt;Setter Property="Template"&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType="{x:Type UserControl}"&gt;
                      &lt;Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true"&gt;
                          &lt;ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/&gt;
                      &lt;/Border&gt;
                      &lt;ControlTemplate.Triggers&gt;
                          &lt;DataTrigger Binding="{Binding IsEditable}" Value="true"&gt;
                              &lt;Setter Property="ContentTemplate" Value="{StaticResource EditableDataTemplate}" /&gt;
                          &lt;/DataTrigger&gt;
                      &lt;/ControlTemplate.Triggers&gt;
                  &lt;/ControlTemplate&gt;
  
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
  &lt;/UserControl.Resources&gt;
  
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>File <strong>TestUserControl.xaml.cs</strong></p>
  
  <pre><code>using System.ComponentModel;
  using System.Windows;
  using System.Windows.Controls;
  
  namespace TestControl
  {
  /// &lt;summary&gt;
  /// Interaction logic for TestControl.xaml
  /// &lt;/summary&gt;
  public partial class TestUserControl : UserControl, INotifyPropertyChanged, INotifyPropertyChanging
  {
      /// &lt;summary&gt;
      /// Constructors
      /// &lt;/summary&gt;
      public TestUserControl()
      {
          InitializeComponent();
      }
  
      // Properties
      private bool _IsEditable;
  
      public bool IsEditable
      {
          get { return _IsEditable; }
          set
          {
  
              _IsEditable = value;
              NotifyPropertyChanged("IsEditable");
          }
      }
  
      // Controls events
      private void SecondButton_Click(object sender, RoutedEventArgs e)
      {
          IsEditable = true;
      }
  
      private void TextBox_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
      {
          if (e.Key == System.Windows.Input.Key.Enter)
              IsEditable = false;
      }
  
      // INotifyPropertyChanged implementation
      public void NotifyPropertyChanged(string propertyName)
      {
          if (PropertyChanged != null)
              PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
      }
  
      public void NotifyPropertyChanging(string propertyName)
      {
          if (PropertyChanging != null)
              PropertyChanging.Invoke(this, new PropertyChangingEventArgs(propertyName));
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      public event PropertyChangingEventHandler PropertyChanging;
  
  
    }
  }
  </code></pre>
  
  <p><strong>MainWindow.xaml</strong></p>
  
  <pre><code>&lt;Window
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:TestControl" x:Class="TestControl.MainWindow"
      Title="MainWindow" Height="170" Width="328"&gt;
      &lt;Grid&gt;
          &lt;local:TestUserControl HorizontalAlignment="Left" Margin="76,57,0,0" VerticalAlignment="Top" Width="148"/&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>and finaly <strong>MainWindow.xaml.cs</strong></p>
  
  <pre><code>using System.Windows;
  
  namespace TestControl
  {
  /// &lt;summary&gt;
  /// Interaction logic for MainWindow.xaml
  /// &lt;/summary&gt;
  public partial class MainWindow : Window
  {
      public MainWindow()
      {
          InitializeComponent();
      }
  }
  }
  </code></pre>
  
  <p>Hope, now it is clear, what I want. I want to change <strong><em>ContentTemplate</em></strong> of my <strong><em>UserControl</em></strong>. This change should be fired by trigger that is watching property <strong>IsEditable</strong>.</p>
  
  <p><strong>Issue</strong></p>
  
  <p>The <strong>IsEditable</strong> property is changing, but the trigger is not firing. Where is the problem?</p>
  
  <p><em>Im sorry for my previous unclear post.</em></p>
  
  <p><em>All Project files possible to download from same location as Figures just change filename to TestControl.zip (cannot post more than two links)</em></p>