WPF 如何解决循环相关的大小调整问题?

发布于 2024-07-29 12:27:39 字数 74 浏览 4 评论 0原文

我试图弄清楚为什么当我设置一个窗口来调整其内容大小,然后设置其内容以占用所有可用空间时,WPF 不会变得疯狂......有什么想法吗?

I'm trying to figure out how come WPF doesn't go crazy when I set a window to resize to its content and then set its content to take all the available space... Any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

神魇的王 2024-08-05 12:27:40

这并不困难:如果窗口设置为拉伸以匹配其内容,则它没有静态大小,因此它采用默认尺寸。 沿着可视树向下走,控件想要拉伸以填充窗口 - 它不会强制任何尺寸,而是接受窗口的任何大小(默认大小)。

我的猜测是,元素总是有一个大小(如果层次结构中没有在元素上方指定,则默认大小)并且提及该大小是固定的还是可协商的。

我这里只是猜测,只是为了证明情况并没有看起来那么糟糕。 有了良好的更新策略,元素就可以以合理的方式相互调整。

It's not that difficult: if the window is set to stretch to match its content, then it doesn't have a static size, so it assumes default dimensions. Going down the visual tree, the control wants to stretch to fill the window -- it doesn't force any dimensions, but instead accepts whatever size the window has (the default one).

My guess is that elements always have a size (default one if none specified above them in the hierarchy) and the mention whether that size is fixed or negotiable.

I'm only guessing here, but it's just to prove that the situation is not as bad as it seems. With a good update strategy in place, the elements can adjust in relation to each other in a sane manner.

岛徒 2024-08-05 12:27:39

布局系统中有两个过程——测量和排列。

首先,在测量过程中,每个子节点都会被赋予一个建议的可用大小,并且这会沿着树向下传播。 这将设置每个子项的 DesiredSize 属性。

在接下来的排列过程中,将考虑 DesiredSize,但父级对将给子级提供多少实际可用大小有最终决定权,并且父级相应地放置每个子级,并通知它的实际大小,依此类推。

另外,请考虑几种可以想象的极端情况:

  • 子级要求 double.Infinity 所需的大小:明确不允许,引发异常
  • 子级将其 Horizo​​ntalAlignment 设置为“拉伸”:父级不提供无限的实际大小
  • 窗口设置为 SizeToConent,子项设置为“Stretch”,没有设置其他约束:
    1. 窗口首先提供无限的度量大小
      • 子级要求其子级测量的大小(文本宽度、边距等) - 不是无限大小
      • 排列:窗口将其大小设置为系统定义的最小值或子级所需的大小,以较大者为准(不能是无限的)
      • 子项会拉伸以适应上一步中确定的尺寸

There are two passes in the layout system - measure and arrange.

First in the measure pass each child is given a proposed available size, and this propagates down the tree. This sets each child's DesiredSize property.

In the next, arrange pass, the DesiredSize is taken into account, but the parent has the final word on how much actual available size it will give to a child, and the parent places each child accordingly, informing it of the actual size it gets, and so on down the tree.

Also, consider several imaginable corner cases:

  • The child demands double.Infinity desired size: explicitly not allowed, raises an exception
  • The child sets its HorizontalAlignment to "Stretch": the parent doesn't offer infinite actual size
  • Window set to SizeToConent, child set to "Stretch", no other constraint set:
    1. Window first offers infinite measure size
      • Child demands the size measured by its children (text width, margins, etc) - not infinite size
      • Arrange: window sets its size to a system-defined minimum or as desired by child, whichever is greater (can't be infinite)
      • The child stretches to fit the size determined in previous step
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文