title: Custom Layout Control and the Logical Tree
body: <p>I've been working on a control for a while that can be used to repeat a layout in our application. It's working great but the problem is that the controls inside the layout control aren't participating in the logical tree.</p>
  
  <p>It's a custom control with a left header, a right header a body and a standard footer. </p>
  
  <p><strong>Generic.xaml:</strong></p>
  
  <pre><code>&lt;Style TargetType="{x:Type local:FrameControl}"&gt;
      &lt;Setter Property="Template"&gt;
          &lt;Setter.Value&gt;
              &lt;ControlTemplate TargetType="{x:Type local:FrameControl}"&gt;
                  &lt;Border Background="{TemplateBinding Background}"
                          BorderBrush="{TemplateBinding BorderBrush}"
                          BorderThickness="{TemplateBinding BorderThickness}"&gt;
                      &lt;Grid&gt;
                          &lt;Border VerticalAlignment="Top" Height="50" Background="LightGray"/&gt;
  
                          &lt;DockPanel Background="{TemplateBinding HeaderColor}"&gt;
                              &lt;ItemsControl ItemsSource="{TemplateBinding HeaderLeftContent}"&gt;
                                  &lt;ItemsControl.ItemsPanel&gt;
                                      &lt;ItemsPanelTemplate&gt;
                                          &lt;StackPanel DockPanel.Dock="Left" FlowDirection="LeftToRight" HorizontalAlignment="Left"/&gt;
                                      &lt;/ItemsPanelTemplate&gt;
                                  &lt;/ItemsControl.ItemsPanel&gt;
                              &lt;/ItemsControl&gt;
  
                              &lt;ItemsControl ItemsSource="{TemplateBinding HeaderRightContent}"&gt;
                                  &lt;ItemsControl.ItemsPanel&gt;
                                      &lt;ItemsPanelTemplate&gt;
                                          &lt;StackPanel DockPanel.Dock="Right" Height="40" 
                                                      Margin="5, 5, 5, 0" VerticalAlignment="Top"
                                                      Orientation="Horizontal" FlowDirection="RightToLeft"/&gt;
                                      &lt;/ItemsPanelTemplate&gt;
                                  &lt;/ItemsControl.ItemsPanel&gt;
                              &lt;/ItemsControl&gt;
                          &lt;/DockPanel&gt;
  
                          &lt;Border BorderThickness="1" BorderBrush="Black" Margin="0,49,0,0" Background="White"&gt;
                              &lt;ItemsControl ItemsSource="{TemplateBinding BodyContent}"&gt;
                                  &lt;ItemsControl.ItemsPanel&gt;
                                      &lt;ItemsPanelTemplate&gt;
                                          &lt;DockPanel Background="White" Margin="5"/&gt;
                                      &lt;/ItemsPanelTemplate&gt;
                                  &lt;/ItemsControl.ItemsPanel&gt;
                              &lt;/ItemsControl&gt;
                          &lt;/Border&gt;
                          &lt;Border VerticalAlignment="Bottom" Height="0" Background="LightGray"/&gt;
                      &lt;/Grid&gt;
                  &lt;/Border&gt;
              &lt;/ControlTemplate&gt;
          &lt;/Setter.Value&gt;
      &lt;/Setter&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p><strong>FrameControl.cs</strong></p>
  
  <pre><code>public class FrameControl : Control
  {
      public Brush HeaderColor
      {
          get { return (Brush)GetValue(HeaderColorProperty); }
          set { SetValue(HeaderColorProperty, value); }
      }
  
      public static readonly DependencyProperty HeaderColorProperty =
          DependencyProperty.Register("HeaderColor", typeof(Brush), typeof(FrameControl), new UIPropertyMetadata(null));
  
      public Collection&lt;Object&gt; HeaderLeftContent
      {
          get { return (Collection&lt;Object&gt;)GetValue(HeaderLeftContentProperty); }
          set { SetValue(HeaderLeftContentProperty, value); }
      }
  
      public static readonly DependencyProperty HeaderLeftContentProperty =
         DependencyProperty.Register("HeaderLeftContent", typeof(Collection&lt;Object&gt;), typeof(FrameControl),
         new UIPropertyMetadata(null));
  
      public Collection&lt;Object&gt; HeaderRightContent
      {
          get { return (Collection&lt;Object&gt;)GetValue(HeaderRightContentProperty); }
          set { SetValue(HeaderRightContentProperty, value); }
      }
  
      public static readonly DependencyProperty HeaderRightContentProperty =
          DependencyProperty.Register("HeaderRightContent", typeof(Collection&lt;Object&gt;), typeof(FrameControl),
          new UIPropertyMetadata(null));
  
      public Collection&lt;Object&gt; BodyContent
      {
          get { return (Collection&lt;Object&gt;)GetValue(BodyContentProperty); }
          set { SetValue(BodyContentProperty, value); }
      }
  
      public static readonly DependencyProperty BodyContentProperty =
          DependencyProperty.Register("BodyContent", typeof(Collection&lt;Object&gt;), typeof(FrameControl),
          new UIPropertyMetadata(null));
  
  
      static FrameControl()
      {
          DefaultStyleKeyProperty.OverrideMetadata(typeof(FrameControl), new FrameworkPropertyMetadata(typeof(FrameControl)));
      }
  
      public FrameControl()
      {
          SetValue(HeaderLeftContentProperty, new Collection&lt;Object&gt;());
          SetValue(HeaderRightContentProperty, new Collection&lt;Object&gt;());
          SetValue(BodyContentProperty, new Collection&lt;Object&gt;());
      }
  }
  </code></pre>
  
  <p><strong>Usage example</strong></p>
  
  <pre><code>   &lt;fc:FrameControl&gt;
          &lt;fc:FrameControl.HeaderLeftContent&gt;
              &lt;Label Content="Left content of the header"/&gt;
          &lt;/fc:FrameControl.HeaderLeftContent&gt;
          &lt;fc:FrameControl.HeaderRightContent&gt;
                  &lt;Button&gt;
                  &lt;TextBlock Text="Example button"/&gt;
              &lt;/Button&gt;
                  &lt;ComboBox SelectedIndex="0"&gt;
                      &lt;ComboBoxItem Content="Filter A"/&gt;
                      &lt;ComboBoxItem Content="Filter B"/&gt;
                      &lt;ComboBoxItem Content="Filter C"/&gt;
                  &lt;/ComboBox&gt;
          &lt;/fc:FrameControl.HeaderRightContent&gt;
          &lt;fc:FrameControl.BodyContent&gt;
              &lt;Grid&gt;
                  &lt;Grid.ColumnDefinitions&gt;
                      &lt;ColumnDefinition Width="Auto"/&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;RowDefinition Height="Auto"/&gt;
                      &lt;RowDefinition Height="Auto"/&gt;
                      &lt;RowDefinition Height="Auto"/&gt;
                  &lt;/Grid.RowDefinitions&gt;
  
                  &lt;Label Grid.Row="0" Grid.Column="0"  Content="name"/&gt;
                  &lt;TextBox Grid.Row="0" Grid.Column="2"/&gt;
  
                  &lt;Label Grid.Row="1" Grid.Column="0"  Content="Type name"/&gt;
                  &lt;TextBox Grid.Row="1" Grid.Column="2" /&gt;
  
                  &lt;Label Grid.Row="2" Grid.Column="0"  Content="Keyword"/&gt;
                  &lt;TextBox Grid.Row="2" Grid.Column="2"/&gt;
  
                  &lt;Label Grid.Row="3" Grid.Column="0"  Content="Parameter"/&gt;
  
                  &lt;ComboBox Grid.Row="4" Grid.Column="0" SelectedIndex="0"&gt;
                      &lt;ComboBoxItem Content="Width"/&gt;
                      &lt;ComboBoxItem Content="Height"/&gt;
                  &lt;/ComboBox&gt;
                  &lt;ComboBox Grid.Row="4" Grid.Column="1" SelectedIndex="0"&gt;
                      &lt;ComboBoxItem Content="..."/&gt;
                      &lt;ComboBoxItem Content="="/&gt;
                      &lt;ComboBoxItem Content="&amp;lt;"/&gt;
                      &lt;ComboBoxItem Content="&amp;gt;"/&gt;
                  &lt;/ComboBox&gt;
                  &lt;TextBox Grid.Row="4" Grid.Column="2"/&gt;
              &lt;/Grid&gt;
          &lt;/fc:FrameControl.BodyContent&gt;
      &lt;/fc:FrameControl&gt;
  </code></pre>
  
  <p>I've tried using different type of properties to hold the elements for the different region like UIElementCollection but that made no difference. I also tried manually adding the controls to the logical tree, but I did not succeed in that. Am I missing something?</p>