更新:2007 年 11 月
命名空间:
System.Windows.Controls 程序集:
PresentationFramework(在 PresentationFramework.dll 中)
用于 XAML 的 XMLNS:http://schemas.microsoft.com/winfx/xaml/presentation
Public Class Grid _
Inherits Panel _
Implements IAddChild
public class Grid : Panel, IAddChild
public ref class Grid : public Panel,
IAddChild
public class Grid extends Panel implements IAddChild
public class Grid extends Panel implements IAddChild
内容模型:Grid 对子内容强制使用强内容模型。有关 Panel 内容模型的更多信息,请参见 Children 属性。
在 Grid 中定义的行和列可以利用 Star 大小调整按比例分配剩余空间。当选择 Star 作为行或列的高度或宽度时,该行或列将得到一个剩余可用空间的加权比例分配。这与 Auto 相反,后者将基于行或列中所含内容的大小均匀分配空间。当您使用 可扩展应用程序标记语言 (XAML) 时,此值表示为 * 或 2*。在第一种情况下,行或列将得到一倍的可用空间,在第二种情况下,行或列将得到两倍的可用空间,依此类推。有关 Star 大小调整的更多信息,请参见使用星型大小调整的示例示例。通过将这种按比例分配空间的技术与 Stretch 的 HorizontalAlignment 和 VerticalAlignment 值结合使用,可以按屏幕空间的百分比来划分布局空间。Grid 是唯一可以按这种方式来分配空间的布局面板。
默认情况下,行和列将采用所需的最少空间量来容纳给定行或列中所含任意单元格中的最大内容。例如,如果某列中的一个单元格含有一个较长的单词,如“hippopotamus”,但该列中的所有其他单元格所含的都是像“dog”这样较短的单词,那么该列的宽度将是最大单词 (hippopotamus) 的宽度。
您可以通过使用 Margin 属性与对齐属性的组合来精确定位 Grid 的子元素。
Grid 的子元素按其在标记或代码中出现的顺序绘制。因而,当各元素共享相同的坐标时,便可以获得分层的顺序(也称为 z 顺序)。
Grid 和 Table 具有一些相同的通用功能,但它们也可以应用于相应方案以便更充分地利用各自的内置功能。Grid 基于行和列的索引来添加元素;而 Table 不采用这种方式。Grid 元素允许内容的分层,其中多个元素可以存在于单个单元格内。Table 不支持分层。Grid 的子元素可以相对于其“单元格”边界的左上角进行绝对定位。Table 不支持此功能。Grid 还提供了比 Table 更灵活的大小调整功能。
Grid 使用 GridLength 对象来定义 RowDefinition 或 ColumnDefinition 的大小调整特征。有关每种单位类型的定义,请参见 GridUnitType。
如果向 Grid 中的某个列添加一个子元素,并且该列的 Width 属性设置为 Auto,则会测量该子元素而不进行任何限制。该行为可在使用了 ScrollViewer 时禁止显示水平滚动条,因为该子元素被测量为“无界”。为了进行显示,该子元素会被裁剪,而不会滚动。
默认情况下,面板元素并不接收焦点。要强制使面板元素接收焦点,请将 Focusable 属性设置为 true。
下面的示例演示如何创建网格。在本例中,网格定义了用来承载子内容的三个 ColumnDefinition 元素和四个 RowDefinition 元素。
Public Sub New()
WindowTitle = "Grid Sample"
'Create a Grid as the root Panel element
Dim myGrid As New Grid()
myGrid.Height = 100
myGrid.Width = 250
myGrid.ShowGridLines = True
myGrid.HorizontalAlignment = Windows.HorizontalAlignment.Left
myGrid.VerticalAlignment = Windows.VerticalAlignment.Top
' Define and Add the Rows and Columns
Dim colDef1 As New ColumnDefinition
Dim colDef2 As New ColumnDefinition
Dim colDef3 As New ColumnDefinition
myGrid.ColumnDefinitions.Add(colDef1)
myGrid.ColumnDefinitions.Add(colDef2)
myGrid.ColumnDefinitions.Add(colDef3)
Dim rowDef1 As New RowDefinition
Dim rowDef2 As New RowDefinition
Dim rowDef3 As New RowDefinition
Dim rowDef4 As New RowDefinition
myGrid.RowDefinitions.Add(rowDef1)
myGrid.RowDefinitions.Add(rowDef2)
myGrid.RowDefinitions.Add(rowDef3)
myGrid.RowDefinitions.Add(rowDef4)
Dim txt1 As New TextBlock
txt1.Text = "2004 Products Shipped"
txt1.FontSize = 20
txt1.FontWeight = FontWeights.Bold
Grid.SetColumnSpan(txt1, 3)
Grid.SetRow(txt1, 0)
myGrid.Children.Add(txt1)
Dim txt2 As New TextBlock
txt2.Text = "Quarter 1"
txt2.FontSize = 12
txt2.FontWeight = FontWeights.Bold
Grid.SetRow(txt2, 1)
Grid.SetColumn(txt2, 0)
myGrid.Children.Add(txt2)
Dim txt3 As New TextBlock
txt3.Text = "Quarter 2"
txt3.FontSize = 12
txt3.FontWeight = FontWeights.Bold
Grid.SetRow(txt3, 1)
Grid.SetColumn(txt3, 1)
myGrid.Children.Add(txt3)
Dim txt4 As New TextBlock
txt4.Text = "Quarter 3"
txt4.FontSize = 12
txt4.FontWeight = FontWeights.Bold
Grid.SetRow(txt4, 1)
Grid.SetColumn(txt4, 2)
myGrid.Children.Add(txt4)
Dim txt5 As New TextBlock
txt5.Text = "50,000"
Grid.SetRow(txt5, 2)
Grid.SetColumn(txt5, 0)
myGrid.Children.Add(txt5)
Dim txt6 As New Controls.TextBlock
txt6.Text = "100,000"
Grid.SetRow(txt6, 2)
Grid.SetColumn(txt6, 1)
myGrid.Children.Add(txt6)
Dim txt7 As New TextBlock
txt7.Text = "150,000"
Grid.SetRow(txt7, 2)
Grid.SetColumn(txt7, 2)
myGrid.Children.Add(txt7)
' Add the final TextBlock Cell to the Grid
Dim txt8 As New TextBlock
txt8.FontSize = 16
txt8.FontWeight = FontWeights.Bold
txt8.Text = "Total Units: 300000"
Grid.SetRow(txt8, 3)
Grid.SetColumnSpan(txt8, 3)
myGrid.Children.Add(txt8)
Me.Content = myGrid
End Sub
// Create the application's main window
mainWindow = new Window();
mainWindow.Title = "Grid Sample";
// Create the Grid
Grid myGrid = new Grid();
myGrid.Width = 250;
myGrid.Height = 100;
myGrid.HorizontalAlignment = HorizontalAlignment.Left;
myGrid.VerticalAlignment = VerticalAlignment.Top;
myGrid.ShowGridLines = true;
// Define the Columns
ColumnDefinition colDef1 = new ColumnDefinition();
ColumnDefinition colDef2 = new ColumnDefinition();
ColumnDefinition colDef3 = new ColumnDefinition();
myGrid.ColumnDefinitions.Add(colDef1);
myGrid.ColumnDefinitions.Add(colDef2);
myGrid.ColumnDefinitions.Add(colDef3);
// Define the Rows
RowDefinition rowDef1 = new RowDefinition();
RowDefinition rowDef2 = new RowDefinition();
RowDefinition rowDef3 = new RowDefinition();
RowDefinition rowDef4 = new RowDefinition();
myGrid.RowDefinitions.Add(rowDef1);
myGrid.RowDefinitions.Add(rowDef2);
myGrid.RowDefinitions.Add(rowDef3);
myGrid.RowDefinitions.Add(rowDef4);
// Add the first text cell to the Grid
TextBlock txt1 = new TextBlock();
txt1.Text = "2005 Products Shipped";
txt1.FontSize = 20;
txt1.FontWeight = FontWeights.Bold;
Grid.SetColumnSpan(txt1, 3);
Grid.SetRow(txt1, 0);
// Add the second text cell to the Grid
TextBlock txt2 = new TextBlock();
txt2.Text = "Quarter 1";
txt2.FontSize = 12;
txt2.FontWeight = FontWeights.Bold;
Grid.SetRow(txt2, 1);
Grid.SetColumn(txt2, 0);
// Add the third text cell to the Grid
TextBlock txt3 = new TextBlock();
txt3.Text = "Quarter 2";
txt3.FontSize = 12;
txt3.FontWeight = FontWeights.Bold;
Grid.SetRow(txt3, 1);
Grid.SetColumn(txt3, 1);
// Add the fourth text cell to the Grid
TextBlock txt4 = new TextBlock();
txt4.Text = "Quarter 3";
txt4.FontSize = 12;
txt4.FontWeight = FontWeights.Bold;
Grid.SetRow(txt4, 1);
Grid.SetColumn(txt4, 2);
// Add the sixth text cell to the Grid
TextBlock txt5 = new TextBlock();
Double db1 = new Double();
db1 = 50000;
txt5.Text = db1.ToString();
Grid.SetRow(txt5, 2);
Grid.SetColumn(txt5, 0);
// Add the seventh text cell to the Grid
TextBlock txt6 = new TextBlock();
Double db2 = new Double();
db2 = 100000;
txt6.Text = db2.ToString();
Grid.SetRow(txt6, 2);
Grid.SetColumn(txt6, 1);
// Add the final text cell to the Grid
TextBlock txt7 = new TextBlock();
Double db3 = new Double();
db3 = 150000;
txt7.Text = db3.ToString();
Grid.SetRow(txt7, 2);
Grid.SetColumn(txt7, 2);
// Total all Data and Span Three Columns
TextBlock txt8 = new TextBlock();
txt8.FontSize = 16;
txt8.FontWeight = FontWeights.Bold;
txt8.Text = "Total Units: " + (db1 + db2 + db3).ToString();
Grid.SetRow(txt8, 3);
Grid.SetColumnSpan(txt8, 3);
// Add the TextBlock elements to the Grid Children collection
myGrid.Children.Add(txt1);
myGrid.Children.Add(txt2);
myGrid.Children.Add(txt3);
myGrid.Children.Add(txt4);
myGrid.Children.Add(txt5);
myGrid.Children.Add(txt6);
myGrid.Children.Add(txt7);
myGrid.Children.Add(txt8);
// Add the Grid as the Content of the Parent Window Object
mainWindow.Content = myGrid;
mainWindow.Show ();
<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" WindowTitle="Grid Sample">
<Grid VerticalAlignment="Top" HorizontalAlignment="Left" ShowGridLines="True" Width="250" Height="100">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock FontSize="20" FontWeight="Bold" Grid.ColumnSpan="3" Grid.Row="0">2005 Products Shipped</TextBlock>
<TextBlock FontSize="12" FontWeight="Bold" Grid.Row="1" Grid.Column="0">Quarter 1</TextBlock>
<TextBlock FontSize="12" FontWeight="Bold" Grid.Row="1" Grid.Column="1">Quarter 2</TextBlock>
<TextBlock FontSize="12" FontWeight="Bold" Grid.Row="1" Grid.Column="2">Quarter 3</TextBlock>
<TextBlock Grid.Row="2" Grid.Column="0">50000</TextBlock>
<TextBlock Grid.Row="2" Grid.Column="1">100000</TextBlock>
<TextBlock Grid.Row="2" Grid.Column="2">150000</TextBlock>
<TextBlock FontSize="16" FontWeight="Bold" Grid.ColumnSpan="3" Grid.Row="3">Total Units: 300000</TextBlock>
</Grid>
</Page>
更多代码
System..::.Object
System.Windows.Threading..::.DispatcherObject
System.Windows..::.DependencyObject
System.Windows.Media..::.Visual
System.Windows..::.UIElement
System.Windows..::.FrameworkElement
System.Windows.Controls..::.Panel
System.Windows.Controls..::.Grid
此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。
Windows Vista
.NET Framework 和 .NET Compact Framework 并不是对每个平台的所有版本都提供支持。有关支持的版本的列表,请参见.NET Framework 系统要求。
.NET Framework
受以下版本支持:3.5、3.0
参考
其他资源