WPF 中的 StackPanel 和 DockPanel 有什么区别?

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

What is the difference between a StackPanel and DockPanel in WPF?

wpf

提问by Casebash

What can a DockPaneldo that a StackPanelcannot? If anyone has an image of something that can be achieved with a StackPanel, but not a DockPanel, than that would be great.

什么DockPanel能做StackPanel而不能做?如果有人有一个可以用StackPanel而不是来实现的东西的图像DockPanel,那就太好了。

回答by Dhaust

Stack Panel: The StackPanel, as the name implies, arranges content either horizontally or vertically. Vertical is the default, but this can be changed using the Orientationproperty. Content is automatically stretched based on the orientation (see screenshot below), and this can be controlled by changing the HorizontalAlignmentor VerticalAlignmentproperties.

堆栈面板StackPanel顾名思义,可以水平或垂直排列内容。Vertical 是默认值,但可以使用该Orientation属性进行更改。内容会根据方向自动拉伸(见下面的截图),这可以通过更改HorizontalAlignmentVerticalAlignment属性来控制。

StackPanel example screenshot

StackPanel 示例截图

Dock Panel: The DockPanelis used to anchor elements to the edges of the container, and is a good choice to set up the overall structure of the application UI. Elements are docked using the DockPanel.Dockattached property. The order that elements are docked determines the layout.

Dock PanelDockPanel用于将元素锚定到容器的边缘,是设置应用程序 UI 整体结构的不错选择。元素使用DockPanel.Dock附加属性停靠。元素停靠的顺序决定了布局。

DockPanel example screenshot

DockPanel 示例截图