title: Two-way binding on a ContentControl
body: <p>I'm creating a Key/Value pair  editor and would like for the value to have a custom template based on the data type.</p>
  
  <pre><code>&lt;TextBox x:Uid="txtKey" x:Name="txtKey" Grid.Column="1" Grid.Row="0" Text="{Binding ElementName=This, Path=KeyValuePair.Key}" VerticalAlignment="Top"/&gt;
  &lt;ContentControl Grid.Column="1" Grid.Row="1"
             x:Uid="ContentControl1" x:Name="ContentControl1" 
             Content="{Binding ElementName=This, Path=KeyValuePair.Value}" 
             LostFocus="ContentControl1_LostFocus"  
             DataContextChanged="ContentControl1_DataContextChanged"   /&gt;
  </code></pre>
  
  <p>The codebehind for the host class looks like this:</p>
  
  <pre><code>public partial class KeyValueControl : ControlBase
  {
      private System.Collections.DictionaryEntry _dictionaryEntry;
      private KeyValuePairObjectObject _KeyValuePair = new KeyValuePairObjectObject();
      private DataTemplate _editorDataTemplate;
      private Caelum.Libraries.Ui.Editors.Resources resources = new Editors.Resources();
  
      public DataTemplate EditorDataTemplate
      {
          get { return _editorDataTemplate; }
          set { _editorDataTemplate = value; SendPropertyChanged("EditorDataTemplate"); }
      }
  
      public KeyValuePairObjectObject KeyValuePair
      {
          get { return _KeyValuePair; }
          set { _KeyValuePair = value; SendPropertyChanged("KeyValuePair"); }
      }
  
  
      public KeyValueControl()
      {
          InitializeComponent();
          this.DataUpdated += new DataUpdatedHander(KeyValueControl_DataUpdated);
          DataContextChanged += new DependencyPropertyChangedEventHandler(KeyValueControl_DataContextChanged);
      }
  
      void KeyValueControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
      {
      }
  
      public override void Save()
      {
          base.Save();
      }
  
      void KeyValueControl_DataUpdated(object sender, object data)
      {
          if (Data != null)
          {
              _dictionaryEntry = (System.Collections.DictionaryEntry)Data;
              KeyValuePair.Key = _dictionaryEntry.Key;
              KeyValuePair.Value = _dictionaryEntry.Value;
  
              if (KeyValuePair.Value != null)
              {
                  EditorDataTemplate = resources.GetDataTemplate(_dictionaryEntry.Value.GetType());
                  ContentControl1.ContentTemplate = EditorDataTemplate;
              }               
          }
      }
  
  
  }
  </code></pre>
  
  <p>DataTemplates are chosen via the resources class:</p>
  
  <pre><code>public DataTemplate GetDataTemplate(Type type)
      {
  
          if (type == typeof(string))
          {
              return TextInlineEditorTemplate;
          }
          if (type == typeof(bool))
          {
              return BooleanInlineEditorTemplate;
          }
  
          return null;
      }
  </code></pre>
  
  <p>The DataTemplate that is displayed for a string is:</p>
  
  <pre><code>&lt;DataTemplate x:Uid="TextInlineEditorTemplate" x:Key="TextInlineEditorTemplate"  &gt;
      &lt;Grid&gt;
          &lt;TextBox x:Uid="txtTextIET1" x:Name="txtTextIET1" Width="300" Text="{Binding Path=DataContext, Mode=TwoWay, RelativeSource={RelativeSource Self}, BindsDirectlyToSource=True, UpdateSourceTrigger=PropertyChanged}" /&gt;
      &lt;/Grid&gt;
  &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>The data binds OK to the key TextBox (txtKey) and DataTemplate TextBox (txtTextIET1), but changing the value on txtTextIET1 will not trigger the setter on the KeyValuePair property. I've not been able to find any examples of this scenario, so any help would be appreciated.</p>