【问题标题】:wpf grid and stackpanel fillwpf网格和stackpanel填充
【发布时间】:2014-10-22 11:04:09
【问题描述】:

我有一个有 2 行的网格,一行用于名称和按钮,另一行用于输出。

我希望第一行拉伸整个宽度,并且按钮向右对齐。

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

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

【问题讨论】:

  • StackPanel 将填充宽度,但他们的孩子不会。您必须嵌套另一个 Grid 或先用更多单元格扩展 Grid(可能对某些元素使用 RowSpan / ColumnSpan)。

标签: c# wpf


【解决方案1】:

Horizo​​ntal 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>

【讨论】:

  • 感谢您的提示。正是我想要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-21
  • 2010-10-04
  • 2017-10-01
  • 1970-01-01
  • 2011-06-15
  • 1970-01-01
相关资源
最近更新 更多