WPF 如何解决循环相关的大小调整问题?
我试图弄清楚为什么当我设置一个窗口来调整其内容大小,然后设置其内容以占用所有可用空间时,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这并不困难:如果窗口设置为拉伸以匹配其内容,则它没有静态大小,因此它采用默认尺寸。 沿着可视树向下走,控件想要拉伸以填充窗口 - 它不会强制任何尺寸,而是接受窗口的任何大小(默认大小)。
我的猜测是,元素总是有一个大小(如果层次结构中没有在元素上方指定,则默认大小)并且提及该大小是固定的还是可协商的。
我这里只是猜测,只是为了证明情况并没有看起来那么糟糕。 有了良好的更新策略,元素就可以以合理的方式相互调整。
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.
布局系统中有两个过程——测量和排列。
首先,在测量过程中,每个子节点都会被赋予一个建议的可用大小,并且这会沿着树向下传播。 这将设置每个子项的
DesiredSize
属性。在接下来的排列过程中,将考虑
DesiredSize
,但父级对将给子级提供多少实际可用大小有最终决定权,并且父级相应地放置每个子级,并通知它的实际大小,依此类推。另外,请考虑几种可以想象的极端情况:
HorizontalAlignment
设置为“拉伸”:父级不提供无限的实际大小SizeToConent
,子项设置为“Stretch
”,没有设置其他约束: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:
double.Infinity
desired size: explicitly not allowed, raises an exceptionHorizontalAlignment
to "Stretch": the parent doesn't offer infinite actual sizeSizeToConent
, child set to "Stretch
", no other constraint set: