how to calculate the textbock height and width in on load if i create textblock from code?

∥☆過路亽.° 提交于 2019-12-17 09:53:44

问题


TextBlock tbl= new TextBlock();
tbl.text="Kishore";

double x=tbl.ActualHeight;
double y=tbl.ActualWidth;

If i execute the code from the loaded event in Metro - winRT will return 0 for both.

How can I get the ActualWidth in the Loaded or SizeChanged event?


回答1:


Call Measure() then Arrange() and then ActualWidth and ActualHeight will be updated.




回答2:


Can also do this via

UpdateLayout();
testBlock.ActualWidth

This could be useful when calculating multiple objects heights and widths.




回答3:


TextBlock tbl = new TextBlock();
tbl.Text = "Kishore";

tbl.Measure(new Size(0, 0));

double x = tbl.ActualHeight;


来源:https://stackoverflow.com/questions/10556019/how-to-calculate-the-textbock-height-and-width-in-on-load-if-i-create-textblock

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!