如何在 WPF 中获取控件以填充可用空间?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/36108/
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
How to get controls in WPF to fill available space?
提问by Rune Jacobsen
Some WPF controls (like the Button
) seem to happily consume all the available space in its' container if you don't specify the height it is to have.
Button
如果您没有指定它的高度,一些 WPF 控件(如)似乎很乐意消耗其容器中的所有可用空间。
And some, like the ones I need to use right now, the (multiline) TextBox
and the ListBox
seem more worried about just taking the space necessary to fit their contents, and no more.
还有一些,比如我现在需要使用的那些,(多行)TextBox
和ListBox
看起来更担心只是占用必要的空间来容纳它们的内容,仅此而已。
If you put these guys in a cell in a UniformGrid
, they will expand to fit the available space. However, UniformGrid
instances are not right for all situations. What if you have a grid with some rows set to a * height to divide the height between itself and other * rows? What if you have a StackPanel
and you have a Label
, a List
and a Button
, how can you get the list to take up all the space not eaten by the label and the button?
如果您将这些家伙放在 a 中的一个单元格中UniformGrid
,它们将扩展以适应可用空间。但是,UniformGrid
实例并非适用于所有情况。如果您有一个网格,其中一些行设置为 * 高度以划分其自身和其他 * 行之间的高度,该怎么办?如果你有 aStackPanel
并且你有 a Label
、 aList
和 a Button
,你怎么能让列表占据标签和按钮没有占用的所有空间?
I would think this would really be a basic layout requirement, but I can't figure out how to get them to fill the space that they could (putting them in a DockPanel
and setting it to fill also doesn't work, it seems, since the DockPanel
only takes up the space needed by its' subcontrols).
我认为这确实是一个基本的布局要求,但我不知道如何让它们填充它们可以填充的空间(将它们放入 aDockPanel
并将其设置为填充也不起作用,似乎,因为在DockPanel
只占用其子控件所需的空间)。
A resizable GUI would be quite horrible if you had to play with Height
, Width
, MinHeight
, MinWidth
etc.
如果您必须使用、、等Height
,可调整大小的 GUI 会非常糟糕。Width
MinHeight
MinWidth
Can you bind your Height
and Width
properties to the grid cell you occupy? Or is there another way to do this?
您可以将您的Height
和Width
属性绑定到您占据的网格单元吗?或者有另一种方法可以做到这一点?
采纳答案by user3837
Each control deriving from Panel
implements distinct layout logic performed in Measure()
and Arrange()
:
派生自的每个控件都Panel
实现了在Measure()
和 中执行的不同布局逻辑Arrange()
:
Measure()
determines the size of the panel and each of its childrenArrange()
determines the rectangle where each control renders
Measure()
确定面板及其每个子项的大小Arrange()
确定每个控件呈现的矩形
The last child of the DockPanel
fills the remaining space. You can disable this behavior by setting the LastChild
property to false
.
的最后一个孩子DockPanel
填充剩余空间。您可以通过将LastChild
属性设置为 来禁用此行为false
。
The StackPanel
asks each child for its desired size and then stacks them. The stack panel calls Measure()
on each child, with an available size of Infinity
and then uses the child's desired size.
该StackPanel
要求每个孩子的所需的大小,然后堆叠它们。堆栈面板调用Measure()
每个子项,可用大小为Infinity
,然后使用子项所需的大小。
A Grid
occupies all available space, however, it will set each child to their desired size and then center them in the cell.
AGrid
占用所有可用空间,但是,它会将每个子项设置为所需的大小,然后将它们居中放置在单元格中。
You can implement your own layout logic by deriving from Panel
and then overriding MeasureOverride()
and ArrangeOverride()
.
您可以通过派生Panel
并覆盖MeasureOverride()
and来实现自己的布局逻辑ArrangeOverride()
。
See this articlefor a simple example.
有关简单示例,请参阅本文。
回答by Matt Hamilton
There are also some properties you can set to force a control to fill its available space when it would otherwise not do so. For example, you can say:
您还可以设置一些属性来强制控件填充其可用空间,否则它不会这样做。例如,你可以说:
HorizontalContentAlignment="Stretch"
... to force the contents of a control to stretch horizontally. Or you can say:
...强制控件的内容水平拉伸。或者你可以说:
HorizontalAlignment="Stretch"
... to force the control itself to stretch horizontally to fill its parent.
...强制控件本身水平拉伸以填充其父级。
回答by Rune Jacobsen
Well, I figured it out myself, right after posting, which is the most embarassing way. :)
嗯,我自己弄明白了,刚发完,这是最尴尬的方式。:)
It seems every member of a StackPanel will simply fill its minimum requested size.
似乎 StackPanel 的每个成员都会简单地填充其最小请求大小。
In the DockPanel, I had docked things in the wrong order. If the TextBox or ListBox is the only docked item without an alignment, or if they are the last added, they WILL fill the remaining space as wanted.
在 DockPanel 中,我以错误的顺序停靠了东西。如果 TextBox 或 ListBox 是唯一没有对齐的停靠项,或者如果它们是最后添加的,它们将根据需要填充剩余空间。
I would love to see a more elegant method of handling this, but it will do.
我很想看到一种更优雅的方法来处理这个问题,但它确实可以。
回答by urini
Use the HorizontalAlignmentand VerticalAlignmentlayout properties. They control how an element uses the space it has inside its parent when more room is available than it required by the element.
使用HorizontalAlignment和VerticalAlignment布局属性。当可用空间超过元素所需的空间时,它们控制元素如何使用其父元素内部的空间。
The width of a StackPanel, for example, will be as wide as the widest element it contains. So, all narrower elements have a bit of excess space. The alignment properties control what the child element does with the extra space.
例如,StackPanel 的宽度将与它包含的最宽元素一样宽。因此,所有较窄的元素都有一点多余的空间。对齐属性控制子元素对额外空间的作用。
The default value for both properties is Stretch, so the child element is stretched to fill all available space. Additional options include Left, Center and Right for HorizontalAlignmentand Top, Center and Bottom for VerticalAlignment.
这两个属性的默认值都是 Stretch,因此子元素被拉伸以填充所有可用空间。其他选项包括左,中,右为的HorizontalAlignment和顶部,中部和底部VerticalAlignment。