title: Contentpresenter with type based datatemplate selection and binding
body: <p>I have an ItemsControl that binds to a list of items. These items have a name and value property. The value property is of type Object to allow different datatypes to be used. To display the value property correctly I use a ContentPresenter with a datatemplate for every datatype I might use. </p>
  
  <pre><code>  &lt;ItemsControl ItemsSource="{Binding Items}"&gt;
      &lt;ItemsControl.ItemTemplate&gt;
          &lt;DataTemplate&gt;
              &lt;Grid&gt;
                  &lt;Grid.ColumnDefinitions&gt;
                      &lt;ColumnDefinition Width="*"/&gt;
                      &lt;ColumnDefinition Width="Auto" /&gt;
                      &lt;ColumnDefinition Width="*"/&gt;
                  &lt;/Grid.ColumnDefinitions&gt;
  
                  &lt;TextBlock Text="{Binding Path=Name}"/&gt;
  
                  &lt;GridSplitter Width="1" 
                                Grid.RowSpan="4" Grid.Column="1" 
                                HorizontalAlignment="Stretch" VerticalAlignment="Stretch"/&gt;
  
                  &lt;ContentPresenter Grid.Column="2" Content="{Binding Value}"&gt;
                      &lt;ContentPresenter.Resources&gt;
                          &lt;DataTemplate DataType="{x:Type System:String}"&gt;
                              &lt;TextBox Text="{Binding Path=Content, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}" 
                                       BorderThickness="0"/&gt;
                          &lt;/DataTemplate&gt;
                          &lt;DataTemplate DataType="{x:Type System:Int32}"&gt;
                              &lt;TextBox Text="{Binding Path=Content, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}" 
                                       TextAlignment="Right"
                                       BorderThickness="0"/&gt;
                          &lt;/DataTemplate&gt;
                          &lt;DataTemplate DataType="{x:Type System:Double}"&gt;
                              &lt;TextBox Text="{Binding Path=Content, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}" 
                                       TextAlignment="Right"
                                       BorderThickness="0"/&gt;
                          &lt;/DataTemplate&gt;
                          &lt;DataTemplate DataType="{x:Type System:Boolean}"&gt;
                              &lt;CheckBox IsChecked="{Binding Path=Content, RelativeSource={RelativeSource AncestorType={x:Type ContentPresenter}}}"
                                            HorizontalAlignment="Center"/&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/ContentPresenter.Resources&gt;
                  &lt;/ContentPresenter&gt;
              &lt;/Grid&gt;
          &lt;/DataTemplate&gt;
      &lt;/ItemsControl.ItemTemplate&gt;
  &lt;/ItemsControl&gt;
  </code></pre>
  
  <p>The ContentPresenter uses the correct datatype and works great. My problem is that editing these values does not have any effect on the bound items. I suspect it is because I bind to the content property of the ContentPresenter rather than directly to the Value. I've tried using the ContentPresenter like this:</p>
  
  <pre><code>&lt;ContentPresenter Grid.Column="2" Content="{Binding}"&gt;
      &lt;ContentPresenter.Resources&gt;
          &lt;DataTemplate DataType="{x:Type System:String}"&gt;
              &lt;TextBox Text="{Binding Value}" 
                   BorderThickness="0"/&gt;
          &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>But this way the correct DataTemplate isn't selected and it just displays the Object instead of a String for example. I also tried to leave out the path in the binding of the DataTemplate like this:</p>
  
  <pre><code> &lt;DataTemplate DataType="{x:Type System:String}"&gt;
      &lt;TextBox Text="{Binding}" BorderThickness="0"/&gt;
   &lt;/DataTemplate&gt;
  </code></pre>
  
  <p>With this I get an exception telling me to use the Path or XPath attribute. </p>
  
  <p>So my question is: how do I correctly bind to the Value so it displays with the right DataTemplate and that any editing of the values is applied to the bound item. </p>
  
  <p>Btw for some reason the formatted code blocks in my question indent much more after the first line. I tried fixing it but I don't understand what's happening. </p>