.net WPF TextBox 不会填充 StackPanel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3737077/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 14:43:58  来源:igfitidea点击:

WPF TextBox won't fill in StackPanel

.netwpf

提问by Hank

I have a TextBoxcontrol within a StackPanelwhose Orientationis set to Horizontal, but can't get the TextBox to fill the remaining StackPanel space.

我有一个TextBox控件,StackPanelOrientation设置为Horizontal,但无法让 TextBox 填充剩余的 StackPanel 空间。

XAML:

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?

为什么 TextBox 没有填充 StackPanel?

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

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

回答by Zach Johnson

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

我遇到了同样的问题StackPanel,行为是“设计使然”。 StackPanel用于“堆叠”可见区域之外的东西,因此它不允许您填充堆叠维度中的剩余空间。

You can use a DockPanelwith LastChildFillset to trueand dock all the non-filling controls to the Leftto simulate the effect you want.

您可以使用DockPanelwith LastChildFillset totrue并将所有非填充控件停靠到Left来模拟您想要的效果。

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

回答by Bill Moore

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.

解决此问题的另一种方法是将标签堆叠在顶部而不是右侧。我注意到 UWP 有一个内置的 header 属性,您可以使用它,不确定 WPF 是否存在 header 属性。

<TextBox Header="MyLabel" />

回答by Ben

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

我可以使用以下方法用文本框填充 StackPanel:

<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

文本框水平填充 Stackpanel

回答by glihm

Old question by actual topic:

实际主题的旧问题:

HorizontalAlignment="Stretch"

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

是必需的东西。请确保您删除width.