title: How to bind UserControl to content of custom control
body: <p>I am trying to create a very flexible custom control.  The flexibility that I'm trying to achieve to be able to bind <code>UserControl</code> to the <code>ExpanderContent</code> <code>Dependency</code> property, code behind snippet:</p>
  
  <pre><code>public partial class ChartBar : UserControl
  {
      public UIElement ExpanderContent
      {
          get { return (UIElement)GetValue(ExpanderContentProperty); }
          set { SetValue(ExpanderContentProperty, value); }
      }
  
      // Using a DependencyProperty as the backing store for ExpanderContent.  This enables animation, styling, binding, etc...
      public static readonly DependencyProperty ExpanderContentProperty =
          DependencyProperty.Register("ExpanderContent", typeof(UIElement), typeof(ChartBar), new PropertyMetadata(null, OnExpanderContentChanged));
  
      private static void OnExpanderContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
      {
          //throw new NotImplementedException();
      }
      .
      .
      .
  </code></pre>
  
  <p>I have tried using a <code>ContentPresenter</code> in the XAML but it doesn't work.  I obviously can fill with buttons and it works but this defeats the dynamic content via binding.</p>
  
  <pre><code>&lt;Expander x:Name="expander" Header="" VerticalAlignment="Top" d:LayoutOverrides="Width" Style="{DynamicResource ExpanderStyle1}"&gt;
      &lt;ContentPresenter Content="{Binding ExpanderContent, ElementName=TestControlWithContent}" /&gt;
      &lt;!--&lt;WrapPanel HorizontalAlignment="Center" &gt;
          &lt;Button Content="A" Style="{DynamicResource ButtonStyle1}" /&gt;
          &lt;Button Content="B" Style="{DynamicResource ButtonStyle1}" /&gt;
          &lt;Button Content="C" Style="{DynamicResource ButtonStyle1}" /&gt;
          &lt;Button Content="D" Style="{DynamicResource ButtonStyle1}" /&gt;
          &lt;Button Content="E" Style="{DynamicResource ButtonStyle1}" /&gt;
          &lt;Button Content="F" Style="{DynamicResource ButtonStyle1}" /&gt;
      &lt;/WrapPanel&gt;--&gt;
  &lt;/Expander&gt;
  </code></pre>
  
  <p>What's even more confusing is I can do</p>
  
  <pre><code>// ChartBarParent is the name of the custom control set in XAML
  ChartBarParent.Content = new TestControlWithContent();
  </code></pre>
  
  <p>and it works as well as fires the callback.</p>
  
  <p>Ultimately, is <code>UIElement</code> in a dependency property and using a <code>ContentPresenter</code> the right way to do this?</p>