默认情况下,Grid的子元素会自动调整大小占满其所在单元格。你通常不需要为其设置大小,它的大小会根据所在单元格变化而与单元格保持一致。
你也可以通过设置子元素的Margin 属性来设定其与单元格边距距离。
<Grid ShowGridLines="True">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="Larry" Margin="5" Background="Lavender"/>
<Label Grid.Row="0" Grid.Column="1" Content="Moe" Margin="5" Background="Magenta"/>
<Label Grid.Row="0" Grid.Column="2" Content="Curly" Margin="5" Background="Cornsilk"/>
<CheckBox Grid.Row="1" Grid.Column="0" Margin="5" Content="Nose Tweak"/>
<Button Grid.Row="1" Grid.Column="1" Margin="5" Content="Do Eye Poke" />
<TextBox Grid.Row="1" Grid.Column="2" Margin="5" Text="Double slap"/>
</Grid>
原文地址:https://wpf.2000things.com/2011/10/17/409-child-elements-in-a-grid-size-to-fit-the-containing-cell/