XAML 为什么按钮不能拉伸以填充网格单元格?

ss2ws0br  于 2022-12-07  发布在  其他
关注(0)|答案(1)|浏览(119)

我即将在***UWP应用程序***中创建Grid Matrix,但遇到了Button控件无法拉伸以填充可用空间的问题;如何强制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>

ssm49v7z

ssm49v7z1#

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

水平对齐=“拉伸”
垂直对齐=“拉伸”

最终按钮定义:

<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" />

另外,这些属性需要被覆盖的原因是,在Button的默认样式模板中,它们被默认地分别通过Setter设置为Left/Center。

相关问题