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
What is the difference between a StackPanel and DockPanel in WPF?
提问by Casebash
What can a DockPanel
do that a StackPanel
cannot? 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 Orientation
property. Content is automatically stretched based on the orientation (see screenshot below), and this can be controlled by changing the HorizontalAlignment
or VerticalAlignment
properties.
堆栈面板:StackPanel
顾名思义,可以水平或垂直排列内容。Vertical 是默认值,但可以使用该Orientation
属性进行更改。内容会根据方向自动拉伸(见下面的截图),这可以通过更改HorizontalAlignment
或VerticalAlignment
属性来控制。
Dock Panel: The DockPanel
is 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.Dock
attached property. The order that elements are docked determines the layout.
Dock Panel:DockPanel
用于将元素锚定到容器的边缘,是设置应用程序 UI 整体结构的不错选择。元素使用DockPanel.Dock
附加属性停靠。元素停靠的顺序决定了布局。