WPF 跟布局有关的控件很多,System.Windows.Controls.Panel 是这些所有布局有关的类的基类。需要注意的是,我们在页面布局上一般都是使用这个类的扩展类来处理布局,而不是使用这个类。这些扩展类包括:
System.Windows.Controls.Canvas (画布)
System.Windows.Controls.DockPanel (停靠布局)
System.Windows.Controls.Grid (表格)
System.Windows.Controls.StackPanel (堆栈布局)
System.Windows.Controls.VirtualizingPanel (虚堆栈布局)
System.Windows.Controls.WrapPanel (覆盖布局)
ColumnDefinition 和 RowDefinition 分别只需要定义 Width 和 Height
如果我们希望列的宽度或者行的高度是根据内部元素来决定的,我们可以定义为 Auto, 如果我们希望某列或者某行的宽度或者告诉是整体的其他部分,则可以定义成 *,如果我们希望其中一项的长度是另外一项的5倍,则可以一个定义成*,一个定义成5* 。
我们还可以使用 Grid.ColumnSpan Grid.RowSpan 来实现一块布局跨多个表格项的情况。