首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ActualHeight / ActualWidth

ActualHeight / ActualWidth
EN

Stack Overflow用户
提问于 2012-03-14 02:00:44
回答 1查看 1.6K关注 0票数 2

我对ActualWidthActualHeight是如何工作的,或者它是如何计算出来的,感到有点困惑。

代码语言:javascript
复制
<Ellipse Height="30" Width="30" Name="rightHand" Visibility="Collapsed">
    <Ellipse.Fill>
        <ImageBrush ImageSource="Images/Hand.png" />
    </Ellipse.Fill>
</Ellipse>

当我使用上面的代码时,我得到了ActualWidthActualHeight的30。但是当我以编程方式定义一个椭圆时,ActualWidthActualHeight是0,即使我定义了(最大)高度和(最大)宽度属性-我不明白它怎么会是0?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-03-14 02:03:21

ActualWidthActualHeight是在调用MeasureArrange之后计算的。

在将控件插入可视化树之后,WPF的布局系统会自动调用它们(在DispatcherPriority.Render IMHO,这意味着它们将排队等待执行,结果不会立即可用)。

您可以通过在DispatcherPriority.Background上排队操作或手动调用这些方法来等待它们变得可用。

dispatcher变体的示例:

代码语言:javascript
复制
Ellipse ellipse = new Ellipse();

ellipse.Width = 150;
ellipse.Height = 300;

this.grid.Children.Add(ellipse);

this.Dispatcher.BeginInvoke(DispatcherPriority.Background, new Action(() =>
{
    MessageBox.Show(String.Format("{0}x{1}", ellipse.ActualWidth, ellipse.ActualHeight));
}));

显式调用示例:

代码语言:javascript
复制
Ellipse ellipse = new Ellipse();

ellipse.Width = 150;
ellipse.Height = 300;

ellipse.Measure(new Size(1000, 1000));
ellipse.Arrange(new Rect(0, 0, 1000, 1000));

MessageBox.Show(String.Format("{0}x{1}", ellipse.ActualWidth, ellipse.ActualHeight));
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9689513

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档