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


問題描述

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

我在另一個 DockPanel 中有一個 DockPanel,第一個設置為停靠在整個表單上,但第二個設置在表單的頂部,並且它裡面有三個按鈕,它的背景顏色設置為灰色,我可以在編輯器中看到內容藍色邊框但它沒有顏色或文本,當我運行應用程序時什麼都沒有 沒有按鈕 沒有顏色 什麼都沒有。

這是 XAML 代碼:

<Grid Background="White">
    <DockPanel Name="MainBackground">
        <DockPanel Name="Top" Height="32" Background="#FF707070" DockPanel.Dock="Top" Margin="0, 0, 0, 1000">

            <Button
            Width="46"
            Height="32"
            DockPanel.Dock="Right"
            Margin="687,0,0,398"
            Background="White" Click="Button_Click_1">

                <StackPanel Orientation="Horizontal">
                    <Image Source="Res/RDI.png" Width="20" Height="20"/>
                </StackPanel>

            </Button>
            <Button
            Width="46"
            Height="32"
            Content="×" 
            FontSize="20" DockPanel.Dock="Right" Click="Button_Click" Margin="734,0,0,398" Background="White"/>

        </DockPanel>
    </DockPanel>
</Grid>

參考解法

方法 1:

The issue is that you try to position all controls using Margin, which defeats the purpose of a DockPanel. You can select each of the buttons in XAML and look at the designer in Visual Studio. They are all positioned way off. Do not every use this kind of brittle positioning in WPF. There are lots of panels that already take care of that way easier and responsive to resizing.

For example, try the code below. I removed all the Margins and just set the DockPanel.Dock to Right for the buttons. Please note, that you have to set the LastChildFill to false, otherwise the last control placed in the DockPanel will take up the remaining space and is centered in there, regardless of setting a DockPanel.Dock value on it.

If you set the LastChildFill property to true, which is the default setting, the last child element of a DockPanel always fills the remaining space, regardless of any other dock value that you set on the last child element. To dock a child element in another direction, you must set the LastChildFill property to false and must also specify an explicit dock direction on the last child element.

For the outer DockPanel, I just added a new last Grid that takes up the remaining space. If it was not there, you would also have to set the LastChildFill, otherwise the bar would be centered in the window.

<Grid Background="White">
   <DockPanel Name="MainBackground">
      <DockPanel Name="Top" Height="32" Background="#FF707070" DockPanel.Dock="Top" LastChildFill="False">
         <Button
            Width="46"
            Height="32"
            DockPanel.Dock="Right"
            Background="White" Click="Button_Click_1">
            <StackPanel Orientation="Horizontal">
               <Image Source="Res/RDI.png" Width="20" Height="20"/>
            </StackPanel>
         </Button>
         <Button
            Width="46"
            Height="32"
            Content="×" 
            FontSize="20" DockPanel.Dock="Right" Click="Button_Click" Background="White"/>
      </DockPanel>
      <StackPanel>
         <TextBlock Text="This is where your content would be placed."/>
         <TextBlock Text="Alternatively, set the last child fill of the dock panel to false."/>
      </StackPanel>
   </DockPanel>
</Grid>

Now the buttons are automatically positioned to the right, next to each other.

Buttons in a bar at the top of the window content positioned to the right.

Of course, you could create the same layout with other panels as well, but since it is not clear what your final layout should look like, I can only provide this example using your structure.

方法 2:

It's here quite sure but with a margin of Margin="0, 0, 0, 1000" propably it's anywhere.

I think this is the issue.

[EDIT] And I see more of these "3 digit margins". Avoid that, it's not how WPF is meant to be used. Use margins as "a little bit of space around the element"

(by ArterPPthatguyKlamsi)

參考文件

  1. DockPanel is not visible even though it is set to visible (CC BY‑SA 2.5/3.0/4.0)

#dockpanel #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)







留言討論