WPF ToolBar Separator shrinks to nothing when inside a StackPanel

kenwarner picture kenwarner · Aug 13, 2009 · Viewed 11.4k times · Source

Given the very simple wpf app

<Window x:Class="Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="300" Width="800">
    <Grid>
        <ToolBar Height="50" >
            <MenuItem Header="Test1" />
            <MenuItem Header="Test2" />

            <StackPanel Orientation="Horizontal">
                <Separator />
                <MenuItem Header="Test3" />
                <MenuItem Header="Test4" />
                <MenuItem Header="Test5" />
            </StackPanel>
        </ToolBar>
    </Grid>
</Window>

The Separator element shrinks to nothing. If I put the Separator just before the StackPanel begins, it will show up. Why does this happen? Is there a style setting that can be applied somewhere to avoid this?

Answer

Joel B Fant picture Joel B Fant · Aug 13, 2009

The StackPanel is changing the orientation of the Separator somehow. Note that if you explicitly tell the Separator to be 20 units wide, the Separator will be a horizontal line instead of a vertical line. That's part of what's going on.

If you apply a LayoutTransform to the Separator, it undoes whatever the StackPanel is doing.

<Separator>
    <Separator.LayoutTransform>
        <RotateTransform
            Angle="90" />
    </Separator.LayoutTransform>
</Separator>

I don't understand the need for a StackPanel, though.