2014-10-22 73 views
0

我有一个网格,2行一个名称和按钮,另一个输出。wpf网格和堆叠面板填充

我想要第一行拉伸全宽和按钮对齐到右侧。

想知道我缺少什么,因为我认为堆栈面板会填满宽度。

<Window x:Class="Sample.MainWindow" 
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
     Title="MainWindow" Height="350" Width="525"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition Height="Auto"/> 
      <RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 

     <Grid.ColumnDefinitions> 
      <ColumnDefinition Width="*"/> 
     </Grid.ColumnDefinitions> 

     <StackPanel Orientation="Horizontal" Grid.Row="0" Grid.Column="0" > 
      <TextBox Name="NameTextBox" MinWidth="150" HorizontalAlignment="Stretch"/> 
      <Button Margin="10,0" Name="AlertButton" Content="Say Hello" HorizontalAlignment="Stretch" /> 
     </StackPanel> 

     <TextBox Name="OutpuTextBox" MinLines="5" Grid.Row="1" Grid.Column="0"/> 
    </Grid> 
</Window> 
+0

跨越'StackPanel'将填补宽度,但他们的孩子不会。您必须嵌套另一个“网格”或者先用更多的单元格扩展第一个“网格”(也许对某些元素使用“RowSpan”/“ColumnSpan”)。 – Sinatr 2014-10-22 11:11:19

回答

0

水平StackPanel忽略其子女的水平对齐。您可以将您的布局更改为2列,并将在Button第一行的第二列,并设置底部TextBox跨2列

<Grid> 
    <Grid.RowDefinitions> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*"/> 
     <ColumnDefinition Width="Auto"/> 
    </Grid.ColumnDefinitions> 
    <TextBox Name="NameTextBox" MinWidth="150" HorizontalAlignment="Stretch"/> 
    <Button Margin="10,0" Name="AlertButton" Content="Say Hello" HorizontalAlignment="Stretch" Grid.Column="1" /> 
    <TextBox Name="OutpuTextBox" MinLines="5" Grid.Row="1" Grid.ColumnSpan="2"/> 
</Grid> 
+1

感谢您的提示。正是我在找什么。 – 2014-10-22 11:34:14