2017-05-18 78 views
0

我即将在UWP应用程序中创建网格矩阵,并遇到此行为,其中按钮控件未伸展以填充可用空间;相比之下,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

我看到两个按钮拉伸!我看到一个8格的网格,其中前两个填充了文本框,第二个和第三个填充了两个按钮。还有其他4个空单元:https://ibb.co/nLJWfk –

+1

感谢您检查!这可能是因为我忘记提到我在使用Windows通用应用程序(UWP)。仍然很奇怪他们为什么采取不同的行动 – usefulBee

回答

5

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

的Horizo​​ntalAlignment = “拉伸”

VerticalAlignment = “拉伸”

决赛按钮。定义:

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

而且,这些属性需要被覆盖的原因是因为在按钮的默认样式模板它们通过二传手的左/中分别默认设置。

+1

完美的作品!然而,即使默认对齐值设置为拉伸,它仍然非常令人费解,为什么按钮没有默认拉伸。 (https://docs.microsoft.com/en-us/uwp/api/windows.ui.xaml.frameworkelement#Windows_UI_Xaml_FrameworkElement_Horizo​​ntalAlignment) – usefulBee

+3

将'Stretch'的这些属性更改添加到父'Style'模板将会更清晰对于'Button',他已经在父资源中拥有Setters,而不是在每个实例上重复属性。另外,这些属性需要被覆盖的原因是因为在Button的默认样式模板中,默认情况下它们分别通过Setter设置为左/中心。 –