设置共享尺寸组可以使位于不同Grid的两个列的宽度保持一致。设置共享尺寸组的步骤如下:
- 将两个Grid放入一个父容器中,如Grid并设置Grid.IsSharedSizeScope="True"
- 为需要保持宽度一致的列添加SharedSizeGroup="TextLabel"
示例如下:
<Grid Grid.IsSharedSizeScope="True">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid Margin="3" Background="LightYellow" ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="TextLabel"/>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Margin="5">这是共享尺寸组列!!!!</Label>
<Label Grid.Column="1" Margin="5">More text</Label>
<TextBox Grid.Column="2" Margin="5">A text box</TextBox>
</Grid>
<Grid Grid.Row="1" Margin="3" Background="LightYellow" ShowGridLines="True">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" SharedSizeGroup="TextLabel">
</ColumnDefinition>
<ColumnDefinition>
</ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Margin= "5">共享尺寸组列</Label>
<TextBox Grid.Column='1' Margin="5">A text box</TextBox>
</Grid>
</Grid>