【问题标题】:WPF textbox height before its rendered呈现之前的 WPF 文本框高度
【发布时间】:2012-03-19 23:53:35
【问题描述】:

如何在渲染对象之前获取文本框的实际高度 在 wpf 中?

以下代码可以在 Windows 中运行,但在 WPF 中等效的是什么?

textBox1.Text = "Test";  
FontFamily fontFamily = new FontFamily("Arial");  
float FontLineSpacing = fontFamily.GetLineSpacing(FontStyle.Regular);  

// Get the em height of the font family in design units.  
int emHeight = fontFamily.GetEmHeight(FontStyle.Regular);  
var Height = (textBox1.Font.Size * FontLineSpacing / emHeight) + 7;

【问题讨论】:

  • 我已经编辑了你的问题 - 请下次使用干净的语言,避免使用重复的问号,这对任何人没有帮助。
  • 您是否要获取文本的大小?还是文本框的大小?
  • 如果是TextBox 的高度,那么textBox1.ActualHeight 就是你想要的。
  • 感谢 Shai,我需要文本框的实际高度,但在渲染到画布或网格之前。 textBox1.ActualHeight 有效,但在绘制或渲染之后,而不是之前。

标签: c# wpf textbox height


【解决方案1】:

您可以在访问其ActualHeight 之前调用元素的UpdateLayout() 方法。

例如txtBlock.UpdateLayout()。

【讨论】:

    猜你喜欢
    • 2010-12-16
    • 1970-01-01
    • 2011-03-27
    • 1970-01-01
    • 2011-08-14
    • 1970-01-01
    • 2011-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多