【问题标题】:ActualHeight / ActualWidth实际高度/实际宽度
【发布时间】:2012-03-13 18:00:44
【问题描述】:

我对 ActualWidthActualHeight 的工作原理或计算方式有点困惑。

<Ellipse Height="30" Width="30" Name="rightHand" Visibility="Collapsed">
    <Ellipse.Fill>
        <ImageBrush ImageSource="Images/Hand.png" />
    </Ellipse.Fill>
</Ellipse>

当我使用上面的代码时,ActualWidthActualHeight 得到 30。但是当我以编程方式定义椭圆时,ActualWidthActualHeight 为 0,即使我定义了 (max)height 和 (max)width 属性 - 我不明白它怎么可能是 0?

【问题讨论】:

    标签: c# wpf actualwidth actualheight


    【解决方案1】:

    ActualWidthActualHeight 在调用 MeasureArrange 之后计算。

    将控件插入可视化树后,WPF 的布局系统会自动调用它们(DispatcherPriority.Render 恕我直言,这意味着它们将排队等待执行,结果不会立即可用)。
    您可以通过在DispatcherPriority.Background 排队操作或手动调用方法来等待它们可用。

    调度程序变体的示例:

    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));
    }));
    

    显式调用示例:

    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));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-09-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-10-08
      • 2011-05-31
      • 2015-05-25
      • 2015-04-27
      相关资源
      最近更新 更多