title: WPF "Cannot find source for binding" with ContentPresenters, and UserControl
body: <p>I have a:</p>
  
  <blockquote>
    <p>System.Windows.Data Error: 4 : Cannot find source for binding with
    reference 'ElementName=Test'. BindingExpression:Path=Value;
    DataItem=null; target element is 'Slider' (Name=''); target property
    is 'Value' (type 'Double')</p>
  </blockquote>
  
  <p>error in a very special case.
  I though about a name scope problem but I don't know how to fix it.</p>
  
  <p>Consider the following WPF application :</p>
  
  <p><strong>MyUserControl.xaml</strong>:
  </p>
  
  <pre><code>&lt;UserControl x:Class="WpfApplication1.MyUserControl"
               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="300" d:DesignWidth="300"&gt;
  
      &lt;Grid&gt;
          &lt;Grid.ColumnDefinitions&gt;
              &lt;ColumnDefinition Width="Auto" /&gt;
              &lt;ColumnDefinition Width="*" /&gt;
          &lt;/Grid.ColumnDefinitions&gt;
          &lt;Grid.RowDefinitions&gt;
              &lt;RowDefinition Height="Auto" /&gt;
              &lt;RowDefinition Height="Auto" /&gt;
          &lt;/Grid.RowDefinitions&gt;
  
          &lt;Label Grid.Row="0" Content="Move me !"/&gt;
          &lt;Slider x:Name="Test" Grid.Row="0" Grid.Column="1" /&gt;
          &lt;Label Grid.Row="1" Content="Binded slider"/&gt;
          &lt;ContentPresenter Grid.Row="1" Grid.Column="1"&gt;
              &lt;ContentPresenter.Content&gt;
                  &lt;Slider Value="{Binding Value, ElementName=Test}"/&gt;
              &lt;/ContentPresenter.Content&gt;
          &lt;/ContentPresenter&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p><strong>MyUserControl.xaml.cs</strong>:
  </p>
  
  <pre><code>using System.Windows.Controls;
  
  namespace WpfApplication1
  {
      public partial class MyUserControl : UserControl
      {
          public MyUserControl()
          {
              InitializeComponent();
          }
      }
  }
  </code></pre>
  
  <p><strong>MainWindow.xaml</strong>:
  </p>
  
  <pre><code>&lt;Window x:Class="WpfApplication1.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Width="300" Height="120"&gt;
  
      &lt;StackPanel&gt;
          &lt;Button Click="Button_Click" Content="Click me to show Sliders !" Height="25"/&gt;
          &lt;ContentPresenter x:Name="contentPresenter"/&gt;
      &lt;/StackPanel&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p><strong>MainWindow.xaml.cs</strong>:
  </p>
  
  <pre><code>using System.Windows;
  
  namespace WpfApplication1
  {
      public partial class MainWindow : Window
      {
          private MyUserControl myUserControl;
  
          public MainWindow()
          {
              InitializeComponent();
  
              myUserControl = new MyUserControl();
          }
  
          private void Button_Click(object sender, RoutedEventArgs e)
          {
              contentPresenter.Content = myUserControl;
          }
      }
  }
  </code></pre>
  
  <p>Given the code of MyUserControl.xaml, I expect the binded slider to have the same value has the first one.</p>
  
  <p>But nothing happens.</p>
  
  <p>Now, the tricky part: Start the application, Click on the button, Move the first slider, Open "WPF Inspector" and Attach it to the application. Result: The binding is fixed.</p>
  
  <p>How do you explain this phenomenon?</p>