我在 StackPanel 中的 TextBox 控件中,该控件的 Orientation 设置为 Horizontal ,但无法获取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
发布评论