Gerard
Gerard

Reputation: 13397

How to change the height of the a WPF `<Separator />`?

I use the wpf in a list menuitems in a normal menu (not the context menu).

Using the following style, the separator is not drawn:

<Style x:Key="{x:Static MenuItem.SeparatorStyleKey}" TargetType="{x:Type Separator}">
    <Setter Property="Height" Value="2" />
</Style>

The value of Height must at least be 12, but then the distance from menuitems is too large.

What is happening here? Is it logical? Is there a solution?

Upvotes: 4

Views: 13138

Answers (3)

Omar Bousbia
Omar Bousbia

Reputation: 123

Simply scale the separator on the Y axis

 <Separator>
    <Separator.RenderTransform>
        <ScaleTransform ScaleY="3" />
     </Separator.RenderTransform>
 </Separator>

This will set the separator's height to 3 times the original one.

see this: How to: Scale an Element

Upvotes: 8

Nytmo
Nytmo

Reputation: 71

Use negative margin:

<Style x:Key="{x:Static MenuItem.SeparatorStyleKey}" TargetType="{x:Type Separator}">
    <Setter Property="Margin" Value="-4,0,-3,0" />
</Style>

Upvotes: 0

Sheridan
Sheridan

Reputation: 69979

You can use the Margin property to size and/or space the Separator element to a degree:

<StackPanel>
    <Button Width="100" Content="Click me" />
    <Separator Margin="50,20" />
    <Button Width="100" Content="Click me too" />
</StackPanel>

In general, its length will fill the available area, while its width will remain at one pixel, or vice versa depending on its orientation. This will affect its Width:

<StackPanel>
    <Button Width="100" Content="Click me" />
    <Separator Margin="20" Width="20" />
    <Button Width="100" Content="Click me too" />
</StackPanel>

This won't affect the Height of the line in this orientation, but it will affect the total space that it takes:

<StackPanel>
    <Button Width="100" Content="Click me" />
    <Separator Margin="20" Height="50" />
    <Button Width="100" Content="Click me too" />
</StackPanel>

If you want more control over the line, then I would recommend that you use the Line Class instead.

Upvotes: 3

Related Questions