C# 如何快速对齐 WPF 窗口中的控件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1040809/
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 can I quickly align the controls in a WPF window?
提问by Csupor Jen?
I noticed that the WPF designer does a very poor job on aligning the controls, compared to the Windows Forms designer.
我注意到与 Windows 窗体设计器相比,WPF 设计器在对齐控件方面做得很差。
In the window below, I am unable to align each label, so that its text is on the same line as the text in the text box beside. The first label is correctly aligned, but the WPF designer does not give me any snap lines to correctly align the second and the third one.
在下面的窗口中,我无法对齐每个标签,因此其文本与旁边文本框中的文本位于同一行。第一个标签正确对齐,但 WPF 设计器没有给我任何对齐线来正确对齐第二个和第三个标签。
Also, I cannot align the button with the labels. The snap line puts the button a few pixels leftwards compared to the label texts.
此外,我无法将按钮与标签对齐。与标签文本相比,对齐线将按钮向左放置几个像素。
I couldn't find a fast way to do this alignment manually, writing XAML code, either. Putting the controls in a grid, and setting the margin of each control is time consuming.
我也找不到手动进行此对齐的快速方法,即编写 XAML 代码。将控件放在一个网格中,并设置每个控件的边距是很耗时的。
alt text http://img520.imageshack.us/img520/4843/wpfdesigneralignment.png
替代文字 http://img520.imageshack.us/img520/4843/wpfdesigneralignment.png
Do you know a fast way to align the controls in the WPF windows ?
您知道在 WPF 窗口中对齐控件的快速方法吗?
采纳答案by Csupor Jen?
I tought I can avoid doing the alignment with hand-coded XAML. What I ended up with, is this (the styles can be reused in other windows):
我认为我可以避免使用手动编码的 XAML 进行对齐。我最终得到的是这个(样式可以在其他窗口中重复使用):
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" SizeToContent="WidthAndHeight">
<Window.Resources>
<Style x:Key="ControlStyle" TargetType="Control">
<Setter Property="HorizontalAlignment" Value="Left"/>
<Setter Property="VerticalAlignment" Value="Center"/>
</Style>
<Style BasedOn="{StaticResource ControlStyle}" TargetType="Label">
<Setter Property="Margin" Value="-4,0,0,0"/>
</Style>
<Style BasedOn="{StaticResource ControlStyle}" TargetType="TextBox">
<Setter Property="Width" Value="120"/>
</Style>
<Style BasedOn="{StaticResource ControlStyle}" TargetType="Button">
<Setter Property="MinWidth" Value="70"/>
</Style>
<Style TargetType="Grid">
<Setter Property="Margin" Value="10,10,10,10"/>
</Style>
<Style x:Key="SeparatorColumn" TargetType="ColumnDefinition">
<Setter Property="Width" Value="10"/>
</Style>
<Style x:Key="SeparatorRow" TargetType="RowDefinition">
<Setter Property="Height" Value="3"/>
</Style>
</Window.Resources>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition Style="{StaticResource SeparatorColumn}"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition Style="{StaticResource SeparatorRow}"/>
<RowDefinition/>
<RowDefinition Style="{StaticResource SeparatorRow}"/>
<RowDefinition/>
<RowDefinition Style="{StaticResource SeparatorRow}"/>
<RowDefinition/>
</Grid.RowDefinitions>
<Label Grid.Row="0" Grid.Column="0">Label:</Label>
<TextBox Grid.Row="0" Grid.Column="2">TextBox</TextBox>
<Label Grid.Row="2" Grid.Column="0">Label:</Label>
<TextBox Grid.Row="2" Grid.Column="2">TextBox</TextBox>
<Button Grid.Row="4" Grid.ColumnSpan="3">Button</Button>
<Label Grid.Row="6" Grid.Column="0">Label:</Label>
<TextBox Grid.Row="6" Grid.Column="2">TextBox</TextBox>
</Grid>
</Window>
回答by Justin Niessner
Use a Grid to lay out your controls then make sure you don't have any padding on the controls...unless of course you want some padding and you make sure they're all even.
使用网格来布置控件,然后确保控件上没有任何填充……除非您当然想要一些填充并且确保它们都是均匀的。
A quick Google Search returned a basic tutorial:
快速的 Google 搜索返回了一个基本教程:
回答by Justin Niessner
The best way is to use Grid or DockPanel.
In the most of cases margins are not required.
最好的方法是使用 Grid 或 DockPanel。
在大多数情况下,不需要保证金。
回答by Nathan Clark
Use an appropriate Panel control (StackPanel, DockPanel, etc) for the alignment you are wanting, rather than using the default Grid. The Grid control makes it easy to start dragging controls onto the window wherever you want (without being quite as unstructured as the Canvas), but doesn't make any assumptions about what kind of layout you are actually trying to construct.
使用适当的面板控件(StackPanel、DockPanel 等)进行所需的对齐,而不是使用默认的网格。Grid 控件使您可以轻松地开始将控件拖动到您想要的任何位置(不像 Canvas 那样非结构化),但不会对您实际尝试构建的布局类型做出任何假设。
If the layout you are trying to design is, in fact, a 'grid' (or table, e.g. rows and columns), I suggest either using the UniformGrid control (for evenly-spaced rows and columns) or the Grid with height/width set per row/column and margins on all elements set to 0 (to fully fill its cell).
如果您尝试设计的布局实际上是一个“网格”(或表格,例如行和列),我建议使用 UniformGrid 控件(用于均匀间隔的行和列)或具有高度/宽度的网格设置每行/列,并将所有元素的边距设置为 0(以完全填充其单元格)。
回答by Nir
I've wrote a custom panel that does "form layout" (groups of two columns, all labels same size, all control same size,everything aligned, etc.), it's on my blog at: http://www.nbdtech.com/Blog/archive/2010/07/27/easy-form-layout-in-wpf-part-1-ndash-introducing-formpanel.aspx
我写了一个自定义面板,它执行“表单布局”(两列的组,所有标签大小相同,所有控件大小相同,所有内容都对齐等),它在我的博客上:http://www.nbdtech。 com/Blog/archive/2010/07/27/easy-form-layout-in-wpf-part-1-ndash-introducing-formpanel.aspx
It's designed to be quick and easy to use when editing XAML, I didn't even try it in the designer.
它旨在在编辑 XAML 时快速且易于使用,我什至没有在设计器中尝试过。