如何更改 WPF `<Separator />` 的高度?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19244379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to change the height of the a WPF `<Separator />`?
提问by Gerard
I use the wpf in a list menuitems in a normal menu (not the context menu).
我在普通菜单(不是上下文菜单)的列表菜单项中使用 wpf。
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.
Height 的值必须至少为 12,然后与 menuitems 的距离太大。
What is happening here? Is it logical? Is there a solution?
这里发生了什么?合乎逻辑吗?有解决办法吗?
回答by Omar HeavenSeeker
Simply scale the separator on the Y axis
只需在 Y 轴上缩放分隔符
<Separator>
<Separator.RenderTransform>
<ScaleTransform ScaleY="3" />
</Separator.RenderTransform>
</Separator>
This will set the separator's height to 3 times the original one.
这会将分隔符的高度设置为原始高度的 3 倍。
see this:How to: Scale an Element
请参阅:如何:缩放元素
回答by Sheridan
You can use the Marginproperty to size and/or space the Separatorelement to a degree:
您可以使用该Margin属性Separator在一定程度上调整元素的大小和/或间隔:
<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:
一般来说,它的长度将填满可用区域,而它的宽度将保持在一个像素,反之亦然,这取决于它的方向。这将影响其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 Heightof the line in this orientation, but it willaffect the total space that it takes:
这不会影响Height此方向的线的 ,但会影响它占用的总空间:
<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 LineClassinstead.
如果您想对线路进行更多控制,那么我建议您改用LineClass。
回答by Nytmo
Use negative margin:
使用负边距:
<Style x:Key="{x:Static MenuItem.SeparatorStyleKey}" TargetType="{x:Type Separator}">
<Setter Property="Margin" Value="-4,0,-3,0" />
</Style>

