参考:https://www.cnblogs.com/hushzhang/p/5894021.html
1.Grid.RowDefinitions属性将Grid控件分行,每一个RowDefinition标签将该Grid对象分为一行。
2.Grid.ColumnDefinitions属性将Grid控件分列,每一个ColumnDefinition标签将该Grid对象分为一列。
在给每个分区添加控件时只需指定该控件的Grid.Column和Grid.Row附加属性值,前提是该控件要定义在Grid空间中,否则将找不到这两个属性,也就无法将该控件添加到指定区域中。
当指定的行或列的值大于Grid的单元格数量时,系统默认为最后一个,比如:
<Grid x:Name="LayoutRoot" Background="Blue">
<Grid.ColumnDefinitions>
<ColumnDefinition></ColumnDefinition>
<ColumnDefinition></ColumnDefinition>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
<RowDefinition></RowDefinition>
</Grid.RowDefinitions>
<Button Grid.Column="0" Grid.Row="0" Content=" 0 , 0"></Button>
<Button Grid.Column="1" Grid.Row="1" Content=" 1 , 1"></Button>
<Button Grid.Column="2" Grid.Row="2" Content=" 2 , 2"></Button>
</Grid>
在该示例中,我们将Grid控件分为3行2列,而添加Button时我们却设定其位置分别为(0,0),(1,1),(2,2)。很明显,该Grid并不包含(2,2)–因为它只有2列,但是调试运行时系统并不会报错,而是出现下面的结果:
如果在一行中有两列:
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="2*"/>
表示第一列占1/3,而第二列占2/3。如果直接写“ * ”而前面不带数字,则代表占用剩余所有的空间。
小案例:
<Grid >
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="64"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="26"/>
<RowDefinition Height="26"/>
<RowDefinition Height="210*"/>
<RowDefinition Height="41*"/>
</Grid.RowDefinitions>
<TabControl Grid.Row="2" TabStripPlacement="Right" Grid.ColumnSpan="2" Grid.RowSpan="2">
<TabItem Header="TabItem">
<Grid Background="White">
</Grid>
</TabItem>
<TabItem Header="TabItem">
<Grid Background="#FFE5E5E5"/>
</TabItem>
</TabControl>
</Grid>
运行效果如图所示 :这样做的好处是在界面放大之后图形的画质不会改变
Grid 分割窗口
对于垂直分割,需要将VerticalAlignment属性设置为Stretch,并设置Width为固定值。对于水平分割,需要设置HorizontalAlignment属性设置为Stretch,Height属性设置为固定值。
属性:1.ShowGridLine=True 表示显示分割线
2.GridSpliter类分割窗口可移动,其中ShowsPreview用来设置在拖动分割条时会不会看到一个灰色的阴影跟随鼠标指针。
注意:在把每个控件放到个分区里时最好不要固定高和宽。用Margin来设置。