WPF 的度量大小约束如何工作?
我实现了一个 GUI 系统,其很大程度上受到 WPF 游戏的测量和排列布局系统的启发。虽然它与 DockPanels 和 StackPanels 配合得很好,但我最近需要使用 WrapPanels。我发现,在目前的状态下,我的 GUI 系统无法支持换行,因为我需要知道基于祖先的最大可用大小,而我的测量当前是在没有该信息的情况下完成的。
我检查了 WPF 文档,发现他们的 Measure
方法采用大小约束作为参数:Size Measure(Size availableSize)
。这让我很困惑。根据我的理解,WPF首先进行从叶子到根的测量,然后从根到叶子的排列。然而,在这种情况下,在测量阶段需要来自祖先的可用尺寸。这对我来说似乎是矛盾的,因为祖先的大小取决于他们后代的大小。
WPF 如何在测量过程中提供此 availableSize
值?
I've implemented a GUI system heavily inspired by WPF's Measure and Arrange layout system for a game. Although it works fine with DockPanels and StackPanels, I've recently needed to use WrapPanels. I discovered that, in its present state, my GUI system cannot support wrapping because I would need to know the maximum available size based on the ancestors, and my measurement is currently done without that information.
I've checked the WPF documentation and found out that their Measure
method takes a size constraint as a parameter: Size Measure(Size availableSize)
. This puzzles me. From my understanding of it, WPF first does measurement from the leaves to the root and then the arrangement from the root to the leaves. However, in this instance, the available size from the ancestors is needed while in the measurement phase. This seems contradictory to me as the size of the ancestors depend on the size of their descendants.
How does WPF provide this availableSize
value in the measurement pass?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为您对重新测量的理解是不正确的-它是从根到叶而不是从叶到根发生的。事实上,父控件有责任在其自己的测量过程中对其子控件调用
Measure()
。因此,父级可以计算出有多少空间可供子级使用,并且可以在测量时传递该空间。I think your understanding re measurement is incorrect - it occurs from root to leaves rather than leaves to root. Indeed, it is the parent control's responsibility to call
Measure()
on its children during its own measure pass. Thus, the parent can figure out how much space is available for its children and it can pass that in when measuring.