【问题标题】:How to change the height of the a WPF `<Separator />`?如何更改 WPF `<Separator />` 的高度?
【发布时间】:2013-10-15 04:19:01
【问题描述】:
我在普通菜单(不是上下文菜单)的菜单项列表中使用 wpf。
使用如下样式,不绘制分隔符:
<Style x:Key="{x:Static MenuItem.SeparatorStyleKey}" TargetType="{x:Type Separator}">
<Setter Property="Height" Value="2" />
</Style>
Height 的值必须至少为 12, 但是与 menuitems 的距离太大。
这里发生了什么?合乎逻辑吗?有解决办法吗?
【问题讨论】:
标签:
wpf
xaml
menu
styles
height
【解决方案1】:
在 Y 轴上简单地缩放分隔符
<Separator>
<Separator.RenderTransform>
<ScaleTransform ScaleY="3" />
</Separator.RenderTransform>
</Separator>
这会将分隔符的高度设置为原始高度的 3 倍。
看到这个: How to: Scale an Element
【解决方案2】:
使用负边距:
<Style x:Key="{x:Static MenuItem.SeparatorStyleKey}" TargetType="{x:Type Separator}">
<Setter Property="Margin" Value="-4,0,-3,0" />
</Style>
【解决方案3】:
您可以使用Margin 属性来调整Separator 元素的大小和/或间距:
<StackPanel>
<Button Width="100" Content="Click me" />
<Separator Margin="50,20" />
<Button Width="100" Content="Click me too" />
</StackPanel>
一般来说,它的长度会填满可用区域,而它的宽度将保持在一个像素,反之亦然,具体取决于它的方向。这会影响它的Width:
<StackPanel>
<Button Width="100" Content="Click me" />
<Separator Margin="20" Width="20" />
<Button Width="100" Content="Click me too" />
</StackPanel>
这不会影响此方向的行的Height,但它会影响它占用的总空间:
<StackPanel>
<Button Width="100" Content="Click me" />
<Separator Margin="20" Height="50" />
<Button Width="100" Content="Click me too" />
</StackPanel>
如果您想更好地控制线路,那么我建议您改用Line Class。