【发布时间】:2010-10-08 19:39:52
【问题描述】:
我只想在左侧显示流畅的文本,在右侧显示帮助框。
帮助框应该一直延伸到底部。
如果你去掉下面的外部StackPanel,效果会很好。
但出于布局原因(我正在动态插入 UserControls),我需要包装 StackPanel。
如何让GroupBox 向下延伸到StackPanel 的底部,如您所见,我已经尝试过了:
VerticalAlignment="Stretch"-
VerticalContentAlignment="Stretch" Height="Auto"
XAML:
<Window x:Class="TestDynamic033.Test3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Test3" Height="300" Width="600">
<StackPanel
VerticalAlignment="Stretch"
Height="Auto">
<DockPanel
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Height="Auto"
Margin="10">
<GroupBox
DockPanel.Dock="Right"
Header="Help"
Width="100"
Background="Beige"
VerticalAlignment="Stretch"
VerticalContentAlignment="Stretch"
Height="Auto">
<TextBlock Text="This is the help that is available on the news screen." TextWrapping="Wrap" />
</GroupBox>
<StackPanel DockPanel.Dock="Left" Margin="10" Width="Auto" HorizontalAlignment="Stretch">
<TextBlock Text="Here is the news that should wrap around." TextWrapping="Wrap"/>
</StackPanel>
</DockPanel>
</StackPanel>
</Window>
答案:
感谢 Mark,使用 DockPanel 而不是 StackPanel 清除了它。总的来说,我发现自己现在越来越多地使用 DockPanel 进行 WPF 布局,这是固定的 XAML:
<Window x:Class="TestDynamic033.Test3"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Test3" Height="300" Width="600" MinWidth="500" MinHeight="200">
<DockPanel
VerticalAlignment="Stretch"
Height="Auto">
<DockPanel
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
Height="Auto"
MinWidth="400"
Margin="10">
<GroupBox
DockPanel.Dock="Right"
Header="Help"
Width="100"
VerticalAlignment="Stretch"
VerticalContentAlignment="Stretch"
Height="Auto">
<Border CornerRadius="3" Background="Beige">
<TextBlock Text="This is the help that is available on the news screen." TextWrapping="Wrap"
Padding="5"/>
</Border>
</GroupBox>
<StackPanel DockPanel.Dock="Left" Margin="10" Width="Auto" HorizontalAlignment="Stretch">
<TextBlock Text="Here is the news that should wrap around." TextWrapping="Wrap"/>
</StackPanel>
</DockPanel>
</DockPanel>
</Window>
【问题讨论】:
-
修复了格式 - 它不喜欢直接从列表转到代码
-
你可以让 GroupBox 自行拉伸吗?如果是这样,开始一个一个地添加你的父元素,直到你找出哪个破坏了布局。
-
RoBorg:很高兴知道,这让我很困惑,谢谢
-
谢谢。使用您的答案,我能够使用 2 个嵌套的 DockPanel 来解决我非常相似的问题!
标签: wpf xaml autolayout autoresize dockpanel