WPF 文本框不会填充 StackPanel

我在 StackPanel中有一个 TextBox控件,它的 Orientation被设置为 Horizontal,但是无法让 TextBox 填充剩余的 StackPanel 空间。

XAML:

<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="180" Width="324">


<StackPanel Background="Orange" Orientation="Horizontal" >
<TextBlock Text="a label" Margin="5" VerticalAlignment="Center"/>
<TextBox Height="25" HorizontalAlignment="Stretch" Width="Auto"/>
</StackPanel>
</Window>

这就是它看起来的样子:

alt text

为什么 TextBox 没有填充 StackPanel?

我知道我可以有更多的控制使用 Grid控件,我只是对布局感到困惑。

82921 次浏览

I've had the same problem with StackPanel, and the behavior is "by design". StackPanel is meant for "stacking" things even outside the visible region, so it won't allow you to fill remaining space in the stacking dimension.

You can use a DockPanel with LastChildFill set to true and dock all the non-filling controls to the Left to simulate the effect you want.

<DockPanel Background="Orange" LastChildFill="True">
<TextBlock Text="a label" Margin="5"
DockPanel.Dock="Left" VerticalAlignment="Center"/>
<TextBox Height="25" Width="Auto"/>
</DockPanel>

I would recommend using a Grid instead:

<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="180" Width="324">


<Grid Background="Orange">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="*"/>
</Grid.ColumnDefinitions>


<TextBlock Grid.Column="0" Text="a label"
VerticalAlignment="Center"/>
<TextBox  Grid.Column="1"/>
</Grid>
</Window>

The other way to get around this problem is to stack the label on top instead of to the right. I noticed that UWP has a built in header property you can use for that, not sure if the header property exists for WPF.

<TextBox Header="MyLabel" />

I am able to fill a StackPanel with a textbox using the following:

<StackPanel Margin="5,5,5,5">
<Label Content = "lblExample" Width = "70" Padding="0" HorizontalAlignment="Left"/>
<TextBox Name = "txtExample" Text = "Example Text" HorizontalContentAlignment="Stretch"/>
</StackPanel>

Textbox Horizontally Filling Stackpanel

Old question by actual topic:

HorizontalAlignment="Stretch"

is the required thing. Juste be sure that you remove the width.

Also, eventually take care of styles, it took me a while to figure out that my global TextBox style defined a height so the TextBox didn't stretch. After setting Height="Auto" the TextBox stretched. "Live Property Explorer" is your friend. :)

This probably has annoyed me a couple of times, quite a bit.

The code I started on used a DockPanel, probably because of this matter. But I am not used to it, and it did not seem able to fulfil my needs.

I tried a horizontal StackPanel, but then I ran into this annoying complication.

One would think just something simple needs to be set, or wrapped around. Then it turns out it doesn't. And one ends up trying things and searching the web.

I did not like finicky solutions with code behind, which I came across.

So, as Bimo did, I ended up defining an extra grid for just 2 controls. It is a bit elaborate, but essentially clear and simple.

    <!-- Used an extra grid to get both layout and tabbing right.-->
<Grid Grid.Row="0" Margin="5,5,5,0">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>


<TextBox Grid.Column="0" Name="UriTextBox" Text="{Binding Uri}"/>
<Button Grid.Column="1" Content="Go" Command="{Binding NavigateCommand}" IsDefault="True" Margin="5,0,0,0" Padding="20,0"/>
</Grid>