簡體   English   中英

WPF網格和StackPanel填充

[英]wpf grid and stackpanel fill

我有一個帶有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忽略其子級的水平對齊。 您可以將布局更改為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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM