WPF布局(4) Grid布局容器

Gird布局容器的功能比较强大,能够使用其他布局容器实现的功能,Grid也能实现,Gird是网格式布局,创建Grid面板的布局需要两个步骤:

1.创建希望的行和列的数量。

2为每个包含的控件指定适当的行和列。

下面试XAML 代码:

    <Grid ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Button Grid.Row="0" Grid.Column="0">Button1</Button>
<Button Grid.Row="0" Grid.Column="1">Button2</Button>
<Button Grid.Row="1" Grid.Column="1">Button3</Button>
<Button Grid.Row="1" Grid.Column="2">Button4</Button>
</Grid>


效果图:

 

还可以调整行和列的

Grid面板支持三种设置尺寸的方式:

1.绝对设置尺寸的方式。

2.自动设置尺寸的方式。

3.按比列设置尺寸的方式.

    <Grid ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition Height="*"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition>
</Grid.RowDefinitions>
<TextBox Margin="10" Grid.Row="0">This is a test</TextBox>
<StackPanel Grid.Row="1" HorizontalAlignment="Right" Orientation="Horizontal">
<Button Margin="10,10,2,10" Padding="3">OK</Button>
<Button Margin="2,10,10,10" Padding="3">Cancel</Button>
</StackPanel>
</Grid>

效果图:



posted @ 2011-12-27 23:54  William Jiang  阅读(644)  评论(0编辑  收藏  举报