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

查看:30
本文介绍了如何在 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 中并将其设置为fill 似乎也不起作用,因为 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() 中执行的不同布局逻辑code> 和 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().

参见 这篇文章是一个简单的例子.

See this article for a simple example.

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

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