title: Adding children to UserControl
body: <h3>My task</h3>
  
  <p>Create a <code>UserControl</code> which should be able to contain any visual child which is available in WPF, the children are displayed in a container which is a child of the <code>UserControl</code>.</p>
  
  <h3>My Problem</h3>
  
  <p>I can't manage to get the children displayed correctly in my container, i tried serval ways and did not find a way which works in the designer. I also tried to use <code>ContentControl</code> but nothing gets displayed.</p>
  
  <h3>My approaches</h3>
  
  <p>First i found <a href="http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/48a02a96-93d5-4d5a-bf06-53917be73a52/">this</a> link and i tried it with some variations. I managed to display the content in the right container but it does not work in the designer because the content-property is set-private and the designer want to override it. Placing everything in XAML works but this is not good when working with designers. This is may favorite way.</p>
  
  <p>After this i tried to use <code>ContentControl</code> by binding it's <code>Content</code>-property to a bindable property of the <code>UIElementCollection</code>-type. This aproach is not throwing any errors in the designer, but i have to admit that i never see any control ( e.g. a <code>Button</code> ) in my container. It stays empty but has the children added.</p>
  
  <h3>Conclusion</h3>
  
  <p>After serval hours of searching for a easy and quick solution i decided to ask for solutions here. I'm a little disappointed. It would be really helpful if Microsoft could get a sample into MSDN.</p>
  
  <p>I'm sure there must be a easy way to archive this.</p>
  
  <h3>Current situation</h3>
  
  <p>Thanks to <em>Andrei Gavrila</em> and <em>jberger</em> i archived to create a node which displays the content ( see code below ) but there are still two issues:
  - No designer support
  - The border ( see xaml ) is not shown in designer and not shown when the app is running there is even no margin</p>
  
  <pre><code>public class NodeContent : ContentControl
  {
      static NodeContent()
      {
          DefaultStyleKeyProperty.OverrideMetadata(typeof(NodeContent), new FrameworkPropertyMetadata(typeof(NodeContent)));
      }
  }
  
  public partial class Node : UserControl, INotifyPropertyChanged
  {
      UIElementCollection _Elements;
  
      public event PropertyChangedEventHandler PropertyChanged;
  
      public UIElementCollection NodeContent
      {
          get { return _Elements; }
          set
          {
              _Elements = value;
              OnPropertyChanged("NodeContent");
          }
      }
  
      public Node()
      {
          InitializeComponent();
          NodeContent = new UIElementCollection(NodeContentContainer, this);
      }
  
  
  
      protected void OnPropertyChanged(string name)
      {
          PropertyChangedEventHandler handler = PropertyChanged;
          if (handler != null)
          {
              handler(this, new PropertyChangedEventArgs(name));
          }
      }
  }
  </code></pre>
  
  <p>Node-Xaml:</p>
  
  <pre><code>&lt;UserControl x:Class="Pipedream.Nodes.Node"
               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="216" d:DesignWidth="174" Background="Transparent" Name="NodeControl" xmlns:my="clr-namespace:Pipedream.Nodes"&gt;
  
      &lt;Border BorderThickness="1" CornerRadius="20" BorderBrush="Black" Background="White"&gt;
          &lt;Grid&gt;
              &lt;my:NodeContent x:Name="NodeContentContainer" Margin="20" Content="{Binding Source=NodeControl, Path=NodeContent}" /&gt;
          &lt;/Grid&gt;
      &lt;/Border&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>Generic-Xaml:</p>
  
  <pre><code>&lt;ResourceDictionary
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:Pipedream.Nodes"&gt;
  
  
      &lt;Style TargetType="{x:Type local:NodeContent}"&gt;
          &lt;Setter Property="Template"&gt;
              &lt;Setter.Value&gt;
                  &lt;ControlTemplate TargetType="{x:Type local:Node}"&gt;
                      &lt;Border Background="{TemplateBinding Background}"
                              BorderBrush="{TemplateBinding BorderBrush}"
                              BorderThickness="{TemplateBinding BorderThickness}"&gt;
                          &lt;ContentPresenter /&gt;
                      &lt;/Border&gt;
                  &lt;/ControlTemplate&gt;
              &lt;/Setter.Value&gt;
          &lt;/Setter&gt;
      &lt;/Style&gt;
  &lt;/ResourceDictionary&gt;
  </code></pre>