我即将在 中创建一个网格矩阵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>
最佳答案
只需将这两个属性添加到您的按钮:
HorizontalAlignment="拉伸(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/