ActualHeight/ActualWidth

Mic*_*ael 2 c# wpf actualwidth actualheight

我对如何计算ActualWidthActualHeight工作或如何计算感到困惑.

<Ellipse Height="30" Width="30" Name="rightHand" Visibility="Collapsed">
    <Ellipse.Fill>
        <ImageBrush ImageSource="Images/Hand.png" />
    </Ellipse.Fill>
</Ellipse>
Run Code Online (Sandbox Code Playgroud)

当我使用上面的代码时,我得到30 ActualWidthActualHeight.但是当我以编程方式定义一个椭圆时,即使我定义了(最大)高度和(最大)宽度属性,ActualWidth并且ActualHeight为0,我不明白它是如何为0的?

Mat*_*ias 7

ActualWidthActualHeight在调用Measure和后计算Arrange.

在将控件插入可视树之后,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));
}));
Run Code Online (Sandbox Code Playgroud)

显式调用的示例:

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));
Run Code Online (Sandbox Code Playgroud)