如何让 StackPanel 的孩子向下填充最大空间? - How to get StackPanel's children to fill maximum space downward?

2021年11月20日 阅读数:2
这篇文章主要向大家介绍如何让 StackPanel 的孩子向下填充最大空间? - How to get StackPanel's children to fill maximum space downward?,主要内容包括基础应用、实用技巧、原理机制等方面,希望对大家有所帮助。

问题:

I simply want flowing text on the left, and a help box on the right.我只想要左边的流动文本,右边的帮助框。app

The help box should extend all the way to the bottom.帮助框应该一直延伸到底部。布局

If you take out the outer StackPanel below it works great.若是您取出下方的外部StackPanel ,则效果很好。url

But for reasons of layout (I'm inserting UserControls dynamically) I need to have the wrapping StackPanel .可是出于布局的缘由(我正在动态插入 UserControls),我须要包装StackPanelspa

How do I get the GroupBox to extend down to the bottom of the StackPanel , as you can see I've tried:如何让GroupBox向下延伸到StackPanel的底部,如您所见,我已经尝试过:.net

  • VerticalAlignment="Stretch"
  • VerticalContentAlignment="Stretch"
  • Height="Auto"

XAML: XAML:code

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

Answer:回答:

Thanks Mark, using DockPanel instead of StackPanel cleared it up. DockPanel ,使用DockPanel而不是StackPanel清除了它。 In general, I find myself using DockPanel more and more now for WPF layouting, here's the fixed XAML:通常来讲,我发现本身如今愈来愈多地使用DockPanel进行 WPF 布局,这里是固定的 XAML:xml

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

解决方案:

参考: https://stackoom.com/en/question/2O2x