WPF TextBox不会填写StackPanel

编程入门 行业动态 更新时间:2024-10-21 18:29:27
本文介绍了WPF TextBox不会填写StackPanel的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在 StackPanel 中的 TextBox 控件中,该控件的 Orientation 设置为 Horizo​​ntal ,但无法获取TextBox来填充剩余的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="schemas.microsoft/winfx/2006/xaml/presentation" xmlns:x="schemas.microsoft/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>

这是这样的:

为什么TextBox不填充StackPanel吗?

Why is that TextBox not filling the StackPanel?

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

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

推荐答案

我在 StackPanel ,并且行为是设计使然。 StackPanel 用于在可见区域之外堆叠东西,因此它不允许您填充堆叠维度中的剩余空间。

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.

您可以使用 DockPanel 并将 LastChildFill 设置为 true 并将所有非填充控件停靠在 Left 上,以模拟所需的效果。

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 >

更多推荐

WPF TextBox不会填写StackPanel

本文发布于:2023-11-11 23:28:39,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1579835.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:WPF   TextBox   StackPanel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!