为什么按钮不能填充网格单元?

时间:2017-05-18 18:18:14

标签: xaml uwp

我即将在 UWP应用程序 中创建一个网格矩阵,并遇到这种行为,其中Button控件不会拉伸以填充可用空间;相反,TextBox控件可以。如何强制Button控件的行为类似于TextBox?

     <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <Grid.Resources>
            <Style TargetType="Button">
                <Setter Property="BorderBrush" Value="Yellow" />
                <Setter Property="BorderThickness" Value="5" />
            </Style>

            <Style TargetType="TextBox">
                <Setter Property="BorderBrush" Value="Yellow" />
                <Setter Property="BorderThickness" Value="5" />
            </Style>
        </Grid.Resources>

        <TextBox   Grid.Row="0" Grid.Column="0" Text="One"   />
        <TextBox  Grid.Row="0"  Grid.Column="1" Text="Two"   />

        <Button   Grid.Row="1" Grid.Column="0" Content="Three"   />
        <Button  Grid.Row="1"  Grid.Column="1" Content="Four"   />
    </Grid>

enter image description here

1 个答案:

答案 0 :(得分:7)

只需将这两个属性添加到按钮:

<强>的Horizo​​ntalAlignment =&#34;拉伸&#34;

<强> VerticalAlignment =&#34;拉伸&#34;

最终按钮定义:

<Button
    Grid.Row="1"
    Grid.Column="0"
    HorizontalAlignment="Stretch"
    VerticalAlignment="Stretch"
    Content="Three" />
<Button
    Grid.Row="1"
    Grid.Column="1"
    HorizontalAlignment="Stretch"
    VerticalAlignment="Stretch"
    Content="Four" />
  

此外,需要覆盖这些属性的原因是因为在按钮的默认样式模板中,默认情况下它们分别通过Setter设置为Left / Center。