【问题标题】:how to calculate the textbock height and width in on load if i create textblock from code?如果我从代码创建文本块,如何计算加载时文本块的高度和宽度?
【发布时间】:2012-05-11 17:32:19
【问题描述】:
TextBlock tbl= new TextBlock();
tbl.text="Kishore";

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

如果我从 Metro 中加载的事件中执行代码 - winRT 将为两者返回 0。

如何在LoadedSizeChanged 事件中获得ActualWidth

【问题讨论】:

  • WPF 具有排列/测量等方法,您提供了可用空间并因此获得了尺寸。木星没有类似的东西吗?

标签: c# windows-8 windows-runtime winrt-xaml actualwidth


【解决方案1】:

调用Measure() 然后Arrange() 然后ActualWidthActualHeight 将被更新。

【讨论】:

  • 首先调用Measure,然后调用Arrange。
【解决方案2】:

也可以通过

UpdateLayout();
testBlock.ActualWidth

这在计算多个对象的高度和宽度时可能很有用。

【讨论】:

    【解决方案3】:
    TextBlock tbl = new TextBlock();
    tbl.Text = "Kishore";
    
    tbl.Measure(new Size(0, 0));
    
    double x = tbl.ActualHeight;
    

    【讨论】:

    • 稍微解释一下可能会有所帮助。
    猜你喜欢
    • 2013-10-07
    • 1970-01-01
    • 2023-03-03
    • 2011-05-16
    • 1970-01-01
    • 1970-01-01
    • 2017-07-21
    • 2017-02-23
    • 2012-04-30
    相关资源
    最近更新 更多