如何在WPF中获得控件以填充可用空间? [英] How to get controls in WPF to fill available space?

查看:781
本文介绍了如何在WPF中获得控件以填充可用空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果不指定高度,某些WPF控件(如Button)似乎会很高兴地消耗其容器中的所有可用空间.

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.

还有一些,就像我现在需要使用的那些一样,(多行)TextBoxListBox似乎更担心仅仅占用必要的空间来容纳其内容,而已.

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.

如果将这些人放在UniformGrid的单元格中,它们将扩展以适应可用空间.但是,UniformGrid实例并不适合所有情况.如果您有一个网格,其中某些行设置为*高度,以将其自身与其他*行之间的高度进行划分,该怎么办?如果您有一个StackPanel,并且有一个Label,一个List和一个Button,怎么办?如何获取列表来占用所有未被标签和按钮占用的空间?

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?

我认为这确实是一个基本的布局要求,但是我无法弄清楚如何让它们填充它们可能的空间(将它们放入DockPanel并将其设置为填充也不会似乎可以正常工作,因为DockPanel仅占用了其子控件所需的空间.

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).

如果您不得不玩HeightWidthMinHeightMinWidth等,可调整大小的GUI将会非常可怕.

A resizable GUI would be quite horrible if you had to play with Height, Width, MinHeight, MinWidth etc.

您可以将HeightWidth属性绑定到您占用的网格单元吗?还是有另一种方法可以做到这一点?

Can you bind your Height and Width properties to the grid cell you occupy? Or is there another way to do this?

推荐答案

Panel 派生的每个控件都实现了在Measure()Arrange()中执行的独特布局逻辑:

Each control deriving from Panel implements distinct layout logic performed in Measure() and Arrange():

  • Measure()确定面板及其每个子面板的大小
  • Arrange()确定每个控件渲染的矩形
  • Measure() determines the size of the panel and each of its children
  • Arrange() determines the rectangle where each control renders

DockPanel 的最后一个子项将填充剩余的空间.您可以通过将LastChild属性设置为false来禁用此行为.

The last child of the DockPanel fills the remaining space. You can disable this behavior by setting the LastChild property to false.

StackPanel 要求每个孩子提供所需的尺寸,然后堆叠它们.堆栈面板在每个子项上调用Measure(),可用大小为Infinity,然后使用该子项的所需大小.

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.

Grid 会占用所有可用空间,但是,它将把每个孩子设置为所需的大小,然后将其放在单元格的中心.

A Grid occupies all available space, however, it will set each child to their desired size and then center them in the cell.

您可以通过从Panel派生然后覆盖MeasureOverride()ArrangeOverride()来实现自己的布局逻辑.

You can implement your own layout logic by deriving from Panel and then overriding MeasureOverride() and ArrangeOverride().

请参见本文作为简单示例.

这篇关于如何在WPF中获得控件以填充可用空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆