WPF TextBlock的宽度和高度 [英] WPF TextBlock Width and Height
本文介绍了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.Height
和t.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 becauseActualHeight
is zero until it completes theLoaded > Measured > Arranged > Rendered
cycle. In order to getActualHeight
, you need to subscribe to theSizeChanged
event to get the actual height when it''s set. Basically, in your code, you are getting the value too early.
如果Height
为Auto
,则必须使用ActualHeight
.唯一要记住的是,它仅在呈现控件之后才起作用.因此,如果您尝试在Load
事件期间使用ActualHeight
,它将无法正常工作.
IfHeight
isAuto
, you have to use theActualHeight
. The only thing to keep in mind is, it works only after the control is rendered. So, if you try to useActualHeight
duringLoad
event, it won''t work.
这篇关于WPF TextBlock的宽度和高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文