title: WPF Custom Control - How to properly loop through and display child content
body: <p>Attempting to write a custom control which permits a collection of children content.  The end goal is to have some functional filtering of the child elements based on custom attached dependency properties; however, before getting that far, any attempts I've made to collect and re-display child UIElements have ended in exceptions during XAML parsing/display.</p>
  
  <p>I have a class <code>CustomFilter : Control</code> which has an associated default style in a nearby xaml file.  It has a dependency property for Children, which currently collect the child elements in a UIElementCollection.</p>
  
  <p>I believe my problem may be how I am attempting to render the collection of children.  The only way I know of looping through content in XAML involves using an ItemsControl, to which I am passing my Children collection as the ItemsSource, which feels rather backwards (using UIElements as data models?)</p>
  
  <p>Afterwards, we attempt to render the elements via a content presenter inside of the ItemTemplate.  I've seen in many other examples which render control children using content presenters, so I expect this is probably at least partially correct (though all only using single elements).</p>
  
  <p>The sample class</p>
  
  <pre><code>  [ContentProperty(nameof(Children))]
    public class CustomFilter : Control
    {
      static CustomFilter()
      {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomFilter), new FrameworkPropertyMetadata(typeof(CustomFilter)));
      }
  
      public CustomFilter()
      {
        Children = new UIElementCollection(this, this);
      }
  
      public static readonly DependencyProperty ChildrenProperty = DependencyProperty.Register(nameof(Children), typeof(UIElementCollection), typeof(CustomFilter), new FrameworkPropertyMetadata(RefreshFilter));
      [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
      public UIElementCollection Children
      {
        get { return (UIElementCollection)GetValue(ChildrenProperty); }
        private set { SetValue(ChildrenProperty, value); }
      }
    }
  </code></pre>
  
  <p>The sample template</p>
  
  <pre><code>  &lt;Style TargetType="{x:Type local:CustomFilter}"&gt;
      &lt;Setter Property="Template"&gt;
        &lt;Setter.Value&gt;
          &lt;ControlTemplate TargetType="{x:Type local:CustomFilter}"&gt;
            &lt;StackPanel&gt;
              &lt;ItemsControl ItemsSource="{TemplateBinding Children}"&gt;
                &lt;ItemsControl.ItemTemplate&gt;
                  &lt;DataTemplate&gt;
                    &lt;StackPanel&gt;
                      &lt;ContentPresenter Content="{Binding}"/&gt;
                    &lt;/StackPanel&gt;
                  &lt;/DataTemplate&gt;
                &lt;/ItemsControl.ItemTemplate&gt;
              &lt;/ItemsControl&gt;
            &lt;/StackPanel&gt;
          &lt;/ControlTemplate&gt;
        &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
    &lt;/Style&gt;
  </code></pre>
  
  <p>The use case</p>
  
  <pre><code>  &lt;local:CustomFilter&gt;
      &lt;TextBlock Text="X"/&gt;
      &lt;TextBlock Text="Y"/&gt;
      &lt;TextBlock Text="Z"/&gt;
    &lt;/local:CustomFilter&gt;
  </code></pre>
  
  <p>Attempting to run use this control whatsoever causes it to fail when it reaches the ItemsControl, without reaching the ItemTemplate, with the following exception:</p>
  
  <p><code>ArgumentException: Specified Visual is already a child of another Visual or the root of a CompositionTarget.</code></p>
  
  <p>I've attempted to create a wrapper class for the Children, passing one child to each instance of the wrapper, and binding to that as an ItemsSource - it successfully loops through the ItemTemplate, but attempting to use the ContentPresenter there on the wrapped child element provides me with the following exception instead:</p>
  
  <p><code>ArgumentException: Must disconnect specified child from current parent Visual before attaching to new parent Visual.</code></p>