8

I am trying to create a custom style panel (StyledStackPanel) which is identical to regular StackPanel, that has a custom style. For example - if the style is simply to add an underline, by writing:

<StyledStackPanel>
    <!--User content goes here-->
</StyledStackPanel>

I would like to receive the same appearance as if I wrote:

<StackPanel>
    <!--User content goes here-->
</StackPanel>
<Border Height="1" BorderBrush="Blue"/>

If StackPanel was a Control, I could have replace the DataTemplate of that control. However, it inherits Panel which inherits FrameworkElement.
Is there a way to change the template of a StackPanel?

Elad
  • 19,079
  • 18
  • 62
  • 71

2 Answers2

8

There is no way to set a ControlTemplate for a panel, but you have several options:

  1. Create a custom/user control that has the StackPanel as a child.

  2. Inherit from StackPanel and override OnRender to draw your custom style (you may also need to override MeasureOverride and ArrangeOverride to add a little space around the panel's children for your drawing.

  3. Inherit directly from Panel and override OnRender, MeasureCore and ArrangeCore - if you need to use the panel is a specific way and you don't need all of StackPanel's option this maybe the easiest way since the StackPanel layout algorithm is simple.

  4. Inherit from Decorator (if you do your drawing in code) or ContentControl (if you want to use ControlTemplate) and warp your panel with it when you use it:

         <l:PanelStyle>
             <StackPanel> .. <StackPanel>
         </l:PanelStyle>
    
  5. Write the style for ItemsControl, ItemsControl has a ControlTemplate that you can set and basically wraps a panel (by default it's a vertical StackPanel).

I would go with option 4 if you need access to the StackPanel's properties or 5 if you don't.

Nir
  • 29,306
  • 10
  • 67
  • 103
2

You should make a normal custom control that adds its child elements to an inner StackPanel.

For example:

[ContentProperty("Children")]
public class StyledStackPanel : UserControl {
    public UIElementCollection Children { get { return stackPanel.Children; } }
}

(Untested)

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 4
    The problem here is that all StackPanel-properties that are desired must be rewired. Does exists a simpler solution? Would it help to derive from StackPanel and then do addional stylings with an adorner? – HCL Jul 11 '10 at 13:43