如何使 DockPanel 中的項目擴展以適應 WPF 中的所有可用空間? (How to make items in a DockPanel expand to fit all available space in WPF?)


問題描述

如何使 DockPanel 中的項目擴展以適應 WPF 中的所有可用空間? (How to make items in a DockPanel expand to fit all available space in WPF?)

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:

<pre class="lang-xml prettyprint-override"><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> </pre>


參考解法

方法 1:

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.

方法 2:

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!

<pre class="lang-xml prettyprint-override"><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>
</code></pre>

方法 3:

Set width and height properties to "auto"

(by LM.John WeldonAna BettsJason Watts)

參考文件

  1. How to make items in a DockPanel expand to fit all available space in WPF? (CC BY-SA 3.0/4.0)

#dockpanel #stackpanel #wpf #xaml #C#






相關問題

為什麼 DockPanel.Dock="Bottom" 將元素放在頂部? (Why does DockPanel.Dock="Bottom" put element at the top?)

在 DevExpress 停靠面板中調整內容大小 (Resizing contents inside DevExpress Docking Panels)

DockPanel LastChildFill 調整為 MinWidth? (DockPanel LastChildFill resizing to MinWidth?)

如何更改 DockWindow 上 TitleBar 的背景色? (How to change the Backcolor of TitleBar on DockWindow?)

WPF:HorizontalAlignment=Stretch、MaxWidth 和 Right 同時對齊? (WPF: HorizontalAlignment=Stretch, MaxWidth, and Right aligned at the same time?)

如何使 DockPanel 中的項目擴展以適應 WPF 中的所有可用空間? (How to make items in a DockPanel expand to fit all available space in WPF?)

AvalonDock 的狀態和狀態,AvalonDock 的選項 (State and status of AvalonDock, options to AvalonDock)

如何讓唯一的 DockPanel 子元素停靠在頂部 (How to get sole DockPanel child element to dock on the top)

如果從 Microsoft 的代碼中拋出異常,我如何找出最初導致異常的原因? (How do I find out what originally caused an exception, if it gets thrown from Microsoft's code?)

WPF 拖放面板(如 iGoogle、BBC 網站、blacklight) (WPF Drag Drop Panel (like iGoogle, BBC website, blacklight))

如何使 WPF 文本框填充兩個按鈕之間的所有可用空間? (How to make a WPF TextBox fill all available space between two buttons ?)

DockPanel 不可見,即使它設置為可見 (DockPanel is not visible even though it is set to visible)







留言討論