C# 如何使 DockPanel 中的项目扩展以适应 WPF 中的所有可用空间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/983732/
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-08-06 04:53:49  来源:igfitidea点击:

How to make items in a DockPanel expand to fit all available space in WPF?

c#wpfstackpaneldockpanel

提问by LM.

I have a StackPanelcontaining a StackPaneland some other items. The first StackPanelhas a vertical orientation, the the inner one has a horizontal orientation. The inner one has a TreeViewand a ListView, I would like them to expand and fit the width of the window, which I set by the window and allow the user to change. I would also like the outer StackPanelto fit the height of the window. How do I do this?

我有一个StackPanel包含 aStackPanel和一些其他项目。第一个StackPanel具有垂直方向,内部具有水平方向。内部有 aTreeView和 a ListView,我希望它们扩展并适应窗口的宽度,我由窗口设置并允许用户更改。我还希望外部StackPanel适合窗户的高度。我该怎么做呢?

Edit:I've converted to using a DockPanel, and I've set the DockPanel.Dockproperties correctly in each of the elements, and have disabled LastChildFillin both of the dockpanels, the layout still does not stretch.

编辑:我已经转换为使用 a DockPanel,并且我已经DockPanel.Dock在每个元素中正确设置了属性,并且LastChildFill在两个停靠面板中都禁用了,布局仍然没有拉伸。

The Code:

编码:

<Window x:Class="Clippy.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="400" Width="600" MinHeight="400" MinWidth="600" Loaded="Window_Loaded" SizeChanged="Window_SizeChanged">
    <DockPanel Name="wrapperDockPanel" LastChildFill="False">
        <Menu Height="22" Name="mainMenu" Width="Auto" DockPanel.Dock="Top" />
        <ToolBar Height="26" Name="mainToolBar" Width="Auto" DockPanel.Dock="Top" />
        <DockPanel Height="Auto" Name="contentDockPanel" DockPanel.Dock="Top" LastChildFill="False">
            <TreeView Name="categoryTreeView" />
            <ListView Name="clipListView" />
        </DockPanel>
        <StatusBar Height="23" Name="mainStatusBar" DockPanel.Dock="Top" />
    </DockPanel>
</Window>

采纳答案by Ana Betts

This should do it - I set it up so that the TreeView and the ListView shared the main view 50/50; if you don't want that, set it to 'Auto' and '*' or something. Use "LastChildFill" to your advantage!

这应该可以 - 我设置它以便 TreeView 和 ListView 共享主视图 50/50;如果您不希望那样,请将其设置为“自动”和“*”之类的。使用“LastChildFill”对您有利!

<Window x:Class="Clippy.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="400" Width="600" MinHeight="400" MinWidth="600" Loaded="Window_Loaded" SizeChanged="Window_SizeChanged">

    <DockPanel LastChildFill="True">
        <Menu Width="Auto" DockPanel.Dock="Top" />
        <ToolBar Width="Auto" DockPanel.Dock="Top" />
        <StatusBar DockPanel.Dock="Bottom" />

        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition Height="0.5*" />
                <RowDefinition Height="0.5*" />
            </Grid.RowDefinitions>

            <TreeView Name="categoryTreeView" Grid.Row="0" />
            <ListView Name="clipListView" Grid.Row="1" />
        </Grid>
    </DockPanel>

</Window>

回答by Jason Watts

Set width and height properties to "auto"

将宽度和高度属性设置为“自动”

回答by John Weldon

Use a DockPanel instead. StackPanel explicitly doesn't care about visible space, whereas DockPanel does all of it's size calculation based on available space.

请改用 DockPanel。StackPanel 明确不关心可见空间,而 DockPanel 根据可用空间进行所有大小计算。

Update:

更新:

In addition, in my experience, putting the body of the window into a View, and only having the View in the Window makes for a better Auto Size experience.

另外,根据我的经验,将窗口主体放入一个视图中,并且只有在窗口中包含视图才能获得更好的自动调整大小体验。

For some reason putting all of the children directly into the Window seems to not auto size very well.

出于某种原因,将所有孩子直接放入 Window 似乎不能很好地自动调整大小。

Update 2:

更新 2:

I would remove the explicit DockPanel.Dock attribute from the element that you want to stretch (fill) the unused space.

我会从要拉伸(填充)未使用空间的元素中删除显式 DockPanel.Dock 属性。