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


問題描述

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

下面我在菜單上說“DockPanel.Top”,但它停靠在中間。(?)

如果我將 Height 屬性從 Menu 中取出,它會停靠在頂部,但高度約為 200 像素。(?)

我認為這就是 LastChildFill 的用途。

我怎樣才能讓這個 DockPanel 的子元素停靠在頂部而沒有其他停靠面板中的項目?

<Window x:Class="TestContainer1.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Window1" Height="480" Width="600" Loaded="Window_Loaded">

    <DockPanel HorizontalAlignment="Stretch" 
               Margin="0,0,0,0" 
               Width="Auto" 
               LastChildFill="True">

        <Menu x:Name="panelMenuTop" 
              Width="Auto" 
              Height="25" 
              DockPanel.Dock="Top">

            <MenuItem Header="File">
                <MenuItem Header="Close" 
                          Click="CloseApplication_Click"/>
            </MenuItem>
        </Menu>

    </DockPanel>
</Window>

## 參考解法 #### 方法 1:

Your menu is filling up the entire area so it appears centered. You can just give the Menu a VerticalAlignment="Top" or give the DockPanel some more children as Henk said.

(by Edward TanguayBryan Anderson)

參考文件

  1. How to get sole DockPanel child element to dock on the top (CC BY‑SA 3.0/4.0)

#dockpanel #wpf #xaml






相關問題

為什麼 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)







留言討論