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

发布于 2024-07-23 07:14:39 字数 1406 浏览 9 评论 0原文

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

编辑: 我已转换为使用 DockPanel,并且已在每个元素中正确设置 DockPanel.Dock 属性,并禁用了 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 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>

I have a StackPanel containing a StackPanel and some other items. The first StackPanel has a vertical orientation, the the inner one has a horizontal orientation. The inner one has a TreeView and 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 StackPanel to fit the height of the window. How do I do this?

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

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

疑心病 2024-07-30 07:14:39

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

更新:

此外,根据我的经验,将窗口主体放入视图中,并且仅在窗口中包含视图可以带来更好的自动调整大小体验。

由于某种原因,将所有子项直接放入窗口中似乎不能很好地自动调整大小。

更新 2:

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

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.

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.

Update 2:

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

萌酱 2024-07-30 07:14:39

这应该可以做到 - 我将其设置为 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>

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!

<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>
神回复 2024-07-30 07:14:39

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

Set width and height properties to "auto"

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文