Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
615 views
in Technique[技术] by (71.8m points)

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 >

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...