日期:2014-05-17  浏览次数:20869 次

WPF,麻烦看下这个Grid的布局

 <Grid ShowGridLines="True" Width="auto" Height="auto">
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
            <ColumnDefinition></ColumnDefinition>
        </Grid.ColumnDefinitions>
        <Button Content="Button" Grid.Row="1" Height="23" HorizontalAlignment="Left" Margin="65,131,0,0" Name="button1" VerticalAlignment="Top" Width="75" />
    </Grid>




默认情况下,Grid的高度和宽度是auto的吧,也就是自动设置尺寸方式。其计算规则为:Grid的行高度由该行中元素的最大高度决定,Grid的列宽度由该列中元素的最大宽度决定。
但为什么图中Button周围有那么多空白呢?应该以Button的尺寸为准啊?

------解决方案--------------------
谁告诉你默认是auto的?
Grid的Row和Column默认都是 *(按比例分配)
------解决方案--------------------
<Grid Width="auto" Height="auto">
        <Grid.RowDefinitions>
            <RowDefinition></RowDefinition>
            <RowDefinition Height="auto"></RowDefinition>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="auto"></ColumnDefinition>