如何在呈现前在WPF中测量TextBlock的大小? [英] How do I measure the size of a TextBlock in WPF before it is rendered?

查看:147
本文介绍了如何在呈现前在WPF中测量TextBlock的大小?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WPF DataTemplate,其中有两个TextBlock控件(堆叠),然后是其他一些元素.由于一些复杂的布局代码,我需要知道两个TextBlock元素的高度,以便绘制一些奇特的连接器线,并排列其他控件等.

I have a WPF DataTemplate with two TextBlock controls (stacked) and then some other elements underneath. Due to some complicated layout code, I need to know the height of the two TextBlock elements so that I can draw some fancy connector lines, and line up other controls, etc.

如果我知道进入TextBlocks的文本,并且知道字体等,是否有某种方法可以计算或测量这些TextBlocks的高度而无需实际呈现它们?

If I know the text that's going into the TextBlocks, and I know the font, etc., is there some way I can compute or measure the height of these TextBlocks without actually rendering them?

推荐答案

我认为调用 UIElement.DesiredSize 属性.有关更多信息,请检查提供的MSDN链接.

I think it should be sufficient to call the UIElement.Measure(Size) method and subsequently check the UIElement.DesiredSize property. For more information, check the provided MSDN links.

这篇关于如何在呈现前在WPF中测量TextBlock的大小?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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