WPF TextBox won't fill in StackPanel
.NetWpf.Net Problem Overview
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:
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.
.Net Solutions
Solution 1 - .Net
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 >
Solution 2 - .Net
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 3 - .Net
Old question by actual topic:
HorizontalAlignment="Stretch"
is the required thing. Juste be sure that you remove the width
.
Solution 4 - .Net
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>
Solution 5 - .Net
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 6 - .Net
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>