wpf 如何添加垂直分隔符?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13584998/
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 add a vertical Separator?
提问by Martin Weber
I want to add a vertical Separator to a Grid, but i can only find the horizontal. Isn't there a Property, where you can enter if the line of the separator should be horizontal or vertical?
我想向网格添加垂直分隔符,但我只能找到水平分隔符。是不是有一个属性,如果分隔线应该是水平的还是垂直的,你可以在那里输入?
I searched a lot, but didn't find a short and easy solution to this.
我搜索了很多,但没有找到一个简短而简单的解决方案。
I use .Net Framework 4.0 and Visual Studio Ultimate 2012.
我使用 .Net Framework 4.0 和 Visual Studio Ultimate 2012。
If I try to rotate the horizontal Separator by 90 degrees, it loses the ability to "dock" to other Components.
如果我尝试将水平分隔符旋转 90 度,它将失去“停靠”到其他组件的能力。
The rotated separator looks like this:
旋转后的分隔符如下所示:
<Separator HorizontalAlignment="Left" Height="100" Margin="264,26,0,0" VerticalAlignment="Top" Width="100" RenderTransformOrigin="0.5,0.5">
<Separator.RenderTransform>
<TransformGroup>
<ScaleTransform/>
<SkewTransform/>
<RotateTransform Angle="90"/>
<TranslateTransform/>
</TransformGroup>
</Separator.RenderTransform>
</Separator>
回答by Emmanuel Romulus
This should do exactly what the author wanted:
这应该完全符合作者的要求:
<StackPanel Orientation="Horizontal">
<Separator Style="{StaticResource {x:Static ToolBar.SeparatorStyleKey}}" />
</StackPanel>
if you want a horizontal separator, change the Orientation
of the StackPanel
to Vertical
.
如果需要水平分隔符,请将Orientation
的更改StackPanel
为Vertical
。
回答by Anton Purin
This is not exactly what author asked, but still, it is very simple and works exactly as expected.
这并不完全是作者所要求的,但它仍然非常简单并且完全按预期工作。
Rectangle does the job:
矩形可以完成以下工作:
<StackPanel Grid.Column="2" Orientation="Horizontal">
<Button >Next</Button>
<Button >Prev</Button>
<Rectangle VerticalAlignment="Stretch" Width="1" Margin="2" Stroke="Black" />
<Button>Filter all</Button>
</StackPanel>
回答by Rachel
In the past I've used the style found here
过去我使用过这里的风格
<Style x:Key="VerticalSeparatorStyle"
TargetType="{x:Type Separator}"
BasedOn="{StaticResource {x:Type Separator}}">
<Setter Property="Margin" Value="6,0,6,0"/>
<Setter Property="LayoutTransform">
<Setter.Value>
<TransformGroup>
<TransformGroup.Children>
<TransformCollection>
<RotateTransform Angle="90"/>
</TransformCollection>
</TransformGroup.Children>
</TransformGroup>
</Setter.Value>
</Setter>
</Style>
<Separator Style="{DynamicResource VerticalSeparatorStyle}" />
You need to set the transformation in LayoutTransform
instead of RenderTransform
so the transformation occurs during the Layout pass, not during the Render pass. The Layout pass occurs when WPF is trying to layout controls and figure out how much space each control takes up, while the Render pass occurs after the layout pass when WPF is trying to render controls.
您需要设置转换 inLayoutTransform
而不是RenderTransform
这样转换发生在 Layout pass 期间,而不是在 Render pass 期间。当 WPF 尝试布局控件并确定每个控件占用多少空间时,会发生布局过程,而当 WPF 尝试呈现控件时,会在布局过程之后发生渲染过程。
You can read more about the difference between LayoutTransform
and RenderTransform
hereor here
回答by Kevin K
I like to use the "Line" control. It gives you exact control over where the separator starts and ends. Although it isn't exactly a separator, it functions is the same way, especially in a StackPanel.
我喜欢使用“Line”控件。它使您可以精确控制分隔符的开始和结束位置。虽然它不完全是一个分隔符,但它的功能是相同的,尤其是在 StackPanel 中。
The line control works within a grid too. I prefer to use the StackPanel because you don't have to worry about different controls overlapping.
线控件也适用于网格。我更喜欢使用 StackPanel,因为您不必担心不同的控件重叠。
<StackPanel Orientation="Horizontal">
<Button Content="Button 1" Height="20" Width="70"/>
<Line X1="0" X2="0" Y1="0" Y2="20" Stroke="Black" StrokeThickness="0.5" Margin="5,0,10,0"/>
<Button Content="Button 2" Height="20" Width="70"/>
</StackPanel>
X1 = x starting position (should be 0 for a vertical line)
X1 = x 起始位置(垂直线应为 0)
X2 = x ending position (X1 = X2 for a vertical line)
X2 = x 结束位置(对于垂直线,X1 = X2)
Y1 = y starting position (should be 0 for a vertical line)
Y1 = y 起始位置(垂直线应为 0)
Y2 = y ending position (Y2 = desired line height)
Y2 = y 结束位置(Y2 = 所需的行高)
I use "margin" to add padding on any side of the vertical line. In this instance, there are 5 pixels on the left and 10 pixels on the right of the vertical line.
我使用“边距”在垂直线的任何一侧添加填充。在这种情况下,垂直线左侧有 5 个像素,右侧有 10 个像素。
Since the line control lets you pick the x and y coordinates of the start and end of the line, you can also use it for horizontal lines and lines at any angle in between.
由于线条控件可让您选择线条起点和终点的 x 和 y 坐标,因此您还可以将其用于水平线和其间任意角度的线条。
回答by Connor Mcgrann
This is a very simple way of doing it with no functionality and all visual effect,
这是一种非常简单的方法,没有任何功能和所有视觉效果,
Use a grid and just simply customise it.
使用网格并简单地自定义它。
<Grid Background="DodgerBlue" Height="250" Width="1" VerticalAlignment="Center" Margin="5,0,5,0"/>
Just another way to do it.
只是另一种方式来做到这一点。
回答by Mohimenul Joaa
Vertical separator
垂直分隔符
<Rectangle VerticalAlignment="Stretch" Fill="Blue" Width="1"/>
horizontal separator
水平分隔符
<Rectangle HorizontalAlignment="Stretch" Fill="Blue" Height="4"/>
回答by hyperneu
<Style x:Key="MySeparatorStyle" TargetType="{x:Type Separator}">
<Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.ControlDarkBrushKey}}"/>
<Setter Property="Margin" Value="10,0,10,0"/>
<Setter Property="Focusable" Value="false"/>
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type Separator}">
<Border
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Background="{TemplateBinding Background}"
Height="20"
Width="3"
SnapsToDevicePixels="true"/>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
use
用
<StackPanel Orientation="Horizontal" >
<TextBlock>name</TextBlock>
<Separator Style="{StaticResource MySeparatorStyle}" ></Separator>
<Button>preview</Button>
</StackPanel>
回答by VoteCoffee
Try this example and see if it fits your needs, there are three main aspects to it.
试试这个例子,看看它是否符合你的需求,它有三个主要方面。
Line.Stretch is set to fill.
For horizontal lines the VerticalAlignment of the line is set Bottom, and for VerticalLines the HorizontalAlignment is set to Right.
We then need to tell the line how many rows or columns to span, this is done by binding to either RowDefinitions or ColumnDefintions count property.
<Style x:Key="horizontalLineStyle" TargetType="Line" BasedOn="{StaticResource lineStyle}"> <Setter Property="X2" Value="1" /> <Setter Property="VerticalAlignment" Value="Bottom" /> <Setter Property="Grid.ColumnSpan" Value="{Binding Path=ColumnDefinitions.Count, RelativeSource={RelativeSource AncestorType=Grid}}"/> </Style> <Style x:Key="verticalLineStyle" TargetType="Line" BasedOn="{StaticResource lineStyle}"> <Setter Property="Y2" Value="1" /> <Setter Property="HorizontalAlignment" Value="Right" /> <Setter Property="Grid.RowSpan" Value="{Binding Path=RowDefinitions.Count, RelativeSource={RelativeSource AncestorType=Grid}}"/> </Style> </Grid.Resources> <Grid.RowDefinitions> <RowDefinition Height="20"/> <RowDefinition Height="20"/> <RowDefinition Height="20"/> <RowDefinition Height="20"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="20"/> <ColumnDefinition Width="20"/> <ColumnDefinition Width="20"/> <ColumnDefinition Width="20"/> </Grid.ColumnDefinitions> <Line Grid.Column="0" Style="{StaticResource verticalLineStyle}"/> <Line Grid.Column="1" Style="{StaticResource verticalLineStyle}"/> <Line Grid.Column="2" Style="{StaticResource verticalLineStyle}"/> <Line Grid.Column="3" Style="{StaticResource verticalLineStyle}"/> <Line Grid.Row="0" Style="{StaticResource horizontalLineStyle}"/> <Line Grid.Row="1" Style="{StaticResource horizontalLineStyle}"/> <Line Grid.Row="2" Style="{StaticResource horizontalLineStyle}"/> <Line Grid.Row="3" Style="{StaticResource horizontalLineStyle}"/>
Line.Stretch 设置为填充。
对于水平线,线的 VerticalAlignment 设置为 Bottom,对于 VerticalLines,HorizontalAlignment 设置为 Right。
然后我们需要告诉该行跨越多少行或列,这是通过绑定到 RowDefinitions 或 ColumnDefintions 计数属性来完成的。
<Style x:Key="horizontalLineStyle" TargetType="Line" BasedOn="{StaticResource lineStyle}"> <Setter Property="X2" Value="1" /> <Setter Property="VerticalAlignment" Value="Bottom" /> <Setter Property="Grid.ColumnSpan" Value="{Binding Path=ColumnDefinitions.Count, RelativeSource={RelativeSource AncestorType=Grid}}"/> </Style> <Style x:Key="verticalLineStyle" TargetType="Line" BasedOn="{StaticResource lineStyle}"> <Setter Property="Y2" Value="1" /> <Setter Property="HorizontalAlignment" Value="Right" /> <Setter Property="Grid.RowSpan" Value="{Binding Path=RowDefinitions.Count, RelativeSource={RelativeSource AncestorType=Grid}}"/> </Style> </Grid.Resources> <Grid.RowDefinitions> <RowDefinition Height="20"/> <RowDefinition Height="20"/> <RowDefinition Height="20"/> <RowDefinition Height="20"/> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="20"/> <ColumnDefinition Width="20"/> <ColumnDefinition Width="20"/> <ColumnDefinition Width="20"/> </Grid.ColumnDefinitions> <Line Grid.Column="0" Style="{StaticResource verticalLineStyle}"/> <Line Grid.Column="1" Style="{StaticResource verticalLineStyle}"/> <Line Grid.Column="2" Style="{StaticResource verticalLineStyle}"/> <Line Grid.Column="3" Style="{StaticResource verticalLineStyle}"/> <Line Grid.Row="0" Style="{StaticResource horizontalLineStyle}"/> <Line Grid.Row="1" Style="{StaticResource horizontalLineStyle}"/> <Line Grid.Row="2" Style="{StaticResource horizontalLineStyle}"/> <Line Grid.Row="3" Style="{StaticResource horizontalLineStyle}"/>
回答by Dion Kurczek
Here is how I did it:
这是我如何做到的:
<TextBlock Margin="0,-2,0,0">|</TextBlock>