WPF TextBlock的宽度和高度 [英] WPF TextBlock Width and Height

查看:1103
本文介绍了WPF TextBlock的宽度和高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 当Height"Auto"时,我无法访问TextBlock控件的高度.

Hi I can''t access the Height of TextBlock control when It''s Height is "Auto".

TextBlock t = new TextBlock()
{
    Width = 280,
    TextWrapping = System.Windows.TextWrapping.Wrap,
    FontSize = 12,
    Tag = foundClosureDay.CurrentDate,
    ToolTip = Persians.JulianToHijri(foundClosureDay.CurrentDate).DateToString
};
t.Text = "bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla bla ";

double height = t.Height;

height = t.ActualHeight;




请帮助我,t.Heightt.ActualHeight都不起作用.




Please help me, both t.Height and t.ActualHeight don''t work.

推荐答案

之所以不起作用,是因为ActualHeight 为零直到完成Loaded > Measured > Arranged > Rendered循环.为了获取ActualHeight,您需要订阅SizeChanged事件以获取设置后的实际高度.基本上,在您的代码中,获取该值还为时过早.
The reason this doesn''t work is because ActualHeight is zero until it completes the Loaded > Measured > Arranged > Rendered cycle. In order to get ActualHeight, you need to subscribe to the SizeChanged event to get the actual height when it''s set. Basically, in your code, you are getting the value too early.


如果HeightAuto,则必须使用ActualHeight.唯一要记住的是,它仅在呈现控件之后才起作用.因此,如果您尝试在Load事件期间使用ActualHeight,它将无法正常工作.
If Height is Auto, you have to use the ActualHeight. The only thing to keep in mind is, it works only after the control is rendered. So, if you try to use ActualHeight during Load event, it won''t work.


这篇关于WPF TextBlock的宽度和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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