'WPF TextBox won't fill in StackPanel

I have a TextBox control within a StackPanel whose Orientation is set to Horizontal, but can't get the TextBox to fill the remaining StackPanel space.

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>

And this is what it looks like:

alt text

Why is that TextBox not filling the StackPanel?

I know I can have more control by using a Grid control, I'm just confused about the layout.



Solution 1:[1]

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

Solution 2:[2]

Old question by actual topic:

HorizontalAlignment="Stretch"

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

Solution 3:[3]

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

Solution 4:[4]

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. :)

Solution 5:[5]

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>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 glihm
Solution 3 Ben
Solution 4 IngoB
Solution 5