xaml - 为什么按钮不拉伸(stretch)以填充网格单元格?

标签 xaml uwp

我即将在 中创建一个网格矩阵UWP应用并且遇到了 Button 控件没有拉伸(stretch)以填充可用空间的这种行为;相比之下,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

最佳答案

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

Horizo​​ntalAlignment="拉伸(stretch)"

VerticalAlignment="拉伸(stretch)"

最终按钮定义:

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

Also, the reason those properties need to be overwritten is because in the default Style Template for Button they are set via Setter as Left/Center respectively by default.

关于xaml - 为什么按钮不拉伸(stretch)以填充网格单元格?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44055185/

相关文章:

c# - XAML 样式属性未被继承

c# - 如何防止 UserControl 的内容被覆盖?

.net - 为 .NET Core/UWP 中的单个 HTTP 请求设置自定义 WebProxy

c# - XAML 页面未在 Windows 10 Mobile 上的 WinRT 应用程序中被垃圾收集,但在 WP8.1 上按预期工作

c# - 何时以编程方式检查 Windows Phone 8.1 中的主题更改

c# - XAML ResourceDictionary 无法使用单个键集正确加载

c# - 虚拟化翻转 View 中的 XAML RichTextBlock

javascript - Windows 通用应用程序 - Javascript - 文件 IO - 无响应

c# - 如何使用新属性正确扩展 XAML 控件?

c# - 如何在 UWP 中使用 ListView 捕获点击?