如何在WPF中获取控件以填充可用空间?

本文翻译自:How to get controls in WPF to fill available space?

Some WPF controls (like the Button ) seem to happily consume all the availible space in its' container if you don't specify the height it is to have. 如果你没有指定它的高度,一些WPF控件(比如Button )似乎很乐意消耗它'容器中的所有可用空间。

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. 还有一些,比如我现在需要使用的那些,(多行) TextBoxListBox似乎更担心只需占用适合其内容所需的空间,而不是更多。

If you put these guys in a cell in a UniformGrid , they will expand to fit the available space. 如果你将这些人放在UniformGrid中的单元格中,它们将扩展以适应可用空间。 However, UniformGrid instances are not right for all situations. 但是, UniformGrid实例并不适合所有情况。 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? 如果你有一个StackPanel并且你有一个Label ,一个List和一个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). 我认为这真的是一个基本的布局要求,但我无法弄清楚如何让它们填充他们可能的空间(将它们放入DockPanel并将其设置为填充也不起作用,看起来,因为DockPanel只占用其“子控件”所需的空间。

A resizable GUI would be quite horrible if you had to play with Height , Width , MinHeight , MinWidth etc. 一个可调整大小的GUI将是相当可怕的,如果你有玩HeightWidthMinHeightMinWidth等。

Can you bind your Height and Width properties to the grid cell you occupy? 你可以将你的HeightWidth属性绑定到你占用的网格单元吗? Or is there another way to do this? 或者还有另一种方法吗?


#1楼

参考:https://stackoom.com/question/9oo/如何在WPF中获取控件以填充可用空间


#2楼

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. 在DockPanel中,我以错误的顺序停靠了东西。 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. 如果TextBox或ListBox是唯一没有对齐的停靠项,或者它们是最后添加的,它们将按需填充剩余空间。

I would love to see a more elegant method of handling this, but it will do. 我希望看到一种更优雅的方法来处理这个问题,但它会做到。


#3楼

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. ...强制控件本身水平拉伸以填充其父级。


#4楼

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 children Measure()确定面板及其每个子节点的大小
  • Arrange() determines the rectangle where each control renders Arrange()确定每个控件呈现的矩形

The last child of the DockPanel fills the remaining space. DockPanel的最后一个子项填充剩余空间。 You can disable this behavior by setting the LastChild property to false . 您可以通过将LastChild属性设置为false来禁用此行为。

The StackPanel asks each child for its desired size and then stacks them. StackPanel要求每个孩子获得所需的大小,然后堆叠它们。 The stack panel calls Measure() on each child, with an available size of Infinity and then uses the child's desired size. 堆栈面板调用每个子节点上的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. Grid占用所有可用空间,但是,它会将每个子项设置为所需大小,然后将它们置于单元格中。

You can implement your own layout logic by deriving from Panel and then overriding MeasureOverride() and ArrangeOverride() . 您可以通过从Panel派生然后重写MeasureOverride()ArrangeOverride()来实现自己的布局逻辑。

See this article for a simple example. 有关简单示例,请参阅此文章


#5楼

Use the HorizontalAlignment and VerticalAlignment layout properties. 使用Horizo​​ntalAlignmentVerticalAlignment布局属性。 They control how an element uses the space it has inside its parent when more room is available than it required by the element. 它们控制元素在可用空间多于元素所需的空间时如何使用其父元素内的空间。

The width of a StackPanel, for example, will be as wide as the widest element it contains. 例如,StackPanel的宽度将与它包含的最宽元素一样宽。 So, all narrower elements have a bit of excess space. 因此,所有较窄的元素都有一些过剩的空间。 The alignment properties control what the child element does with the extra space. 对齐属性控制子元素对额外空间的作用。

The default value for both properties is Stretch, so the child element is stretched to fill all available space. 两个属性的默认值是Stretch,因此子元素被拉伸以填充所有可用空间。 Additional options include Left, Center and Right for HorizontalAlignment and Top, Center and Bottom for VerticalAlignment . 其他选项包括左,中,右为的Horizo​​ntalAlignment和顶部,中部和底部VerticalAlignment。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值