在渲染过程之前确定控件的大小
我有一个复合控件,由一个矩形和一些动态创建的标签组成(动态创建,因为在设计期间我不知道应显示多少个标签)。标签的位置(边距)在运行时通过组合整个控件大小、通过依赖属性传递的附加集合以及标签本身的高度来评估。
不幸的是,在渲染标签之前,我没有采用确定性的方法来确定标签高度。 ActualWidth
和 ActualHeight
在显示标签之前为 0,未设置宽度/高度,因为我希望标签根据其内容调整自身大小,DesiredSize< /code> 返回 0,正确的大小或超过实际标签大小的大小(如 2 或 3 倍),
RenderSize
返回有效大小或 0,就像第一个标签返回有效大小,第二个标签返回有效大小- 0.0,没有任何明显的原因。
我尝试过在带有 double.PositiveInfinity 的标签上调用 Measure()
,只是为了达到这种情况,当 DesiredSize
比预期大得多时(所有标签都具有相同的字体)并且仅由数字组成,因此它们都应具有更不相似的大小,但第一个有〜16像素,第二个 -〜36,尽管在渲染之后,RenderSize
对它们都有效)。
是否有一种确定性方法可以仅根据其内容(而不是对齐/边距)在屏幕上呈现之前检查所需的控件大小?
I've got a composite control consisting of a rectangle and a few dynamically created labels (dynamically, because during design time I don't know how many labels shall be displayed). The labels' positions (margins) are evaluated during run time by combining whole control size, additional collection passed through dependency property and by heights of labels themselves.
Unfortunately, I didn't came into deterministic way of determining label height before one was rendered. ActualWidth
and ActualHeight
are 0 before the labels are displayed, Width/Height is not set, because I wish the labels to size themselves basing on their contents, DesiredSize
returns either 0, correct size or size exceeding the real label size (like 2 or 3 times), RenderSize
returns either valid size or 0 and it is like first label returns valid size and second one - 0.0, without any noticeable reason.
I've tried calling Measure()
on the labels with double.PositiveInfinity passed by only to reach situation, when DesiredSize
was way bigger than expected (all labels have the same font and consist only of numbers, so they all shall have more less similar size, but first had ~16 pixels, second - ~36, though after the rendering, RenderSize
was valid for both of them).
Is there a deterministic way to check desired control size, based only on its contents (not on alignment/margins) before it is rendered on screen?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 UpdateLayout 强制采取措施/布局通行证。调用此方法后,
ActualWidth
和ActualHeight
将具有正确的值。You can use UpdateLayout to force a measure / layout pass. After invoking this method
ActualWidth
andActualHeight
will have the correct values.