在这种情况下,为什么 ActualWidth 和 ActualHeight 是 0.0? [英] Why are ActualWidth and ActualHeight 0.0 in this case?

查看:27
本文介绍了在这种情况下,为什么 ActualWidth 和 ActualHeight 是 0.0?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Canvas 中有一个 Grid,定义如下:

I have a Grid inside a Canvas defined like this:

<Canvas x:Name="outerCanvas">
    <Grid Grid.Row="1" Name="cGrid" ShowGridLines="True" Width="{Binding Path=ActualWidth, RelativeSource={RelativeSource AncestorType={x:Type Canvas}}}" Height="{Binding Path=ActualHeight, RelativeSource={RelativeSource AncestorType={x:Type Canvas}}}">
        <Grid.ColumnDefinitions>
            <ColumnDefinition />
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition  />
            <RowDefinition />
        </Grid.RowDefinitions>
        <Rectangle Name="rectangle1" Stroke="Black" Fill="AntiqueWhite" />
        <Rectangle Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="1" Grid.RowSpan="1" Name="rectangle2" Stroke="Black" Fill="AliceBlue" />
        <Rectangle Grid.Row="0" Grid.Column="2" Grid.ColumnSpan="1" Grid.RowSpan="1" Name="rectangle3" Stroke="Black" Fill="Aqua" />
        <Rectangle Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" Grid.RowSpan="1" Name="rectangle4" Stroke="Black" Fill="DarkViolet" />
    </Grid>
</Canvas>

我的问题是,在 Window 构造函数中,在 InitializeComponents() 之后是 Grid.ColumnDefinitions[0].ActualWidth 或任何矩形".ActualWidth 都设置为 0.0(高度相同).我不知道该怎么做才能获得这些信息.有什么帮助吗?

My problem is that, on the Window constructor, after InitializeComponents() either Grid.ColumnDefinitions[0].ActualWidth or "any rectangle".ActualWidth are all set to 0.0 (the same for heights). I'm not figuring out what to do to get this information. Any help?

观察:

  1. 我没有定义外部画布的宽度和高度,但如果我这样做了,它并不能解决我的问题.
  2. 在运行时我可以看到这个Canvas/Grid占据了整个窗口空间,所以里面的每个矩形都有ActualWidths和ActualHeights
  3. 网格的宽度/高度已绑定到画布,但我尝试删除此绑定,但问题仍然存在.
  1. I'm not defining the outer canvas width and height but if I do, it doesn't solve my problem.
  2. At runtime I can see that this Canvas/Grid occupies the entire window space, so every rectangle inside it has ActualWidths and ActualHeights
  3. The grid's width/height is bound to the canvas but I tried removing this binding and my problem still persists.

推荐答案

ActualHeightActualWidth 直到控件被测量和排列.通常 InitializeComponent() 中没有任何东西会导致测量,所以当它返回时,这些仍然是零.

ActualHeight and ActualWidth are not set until the control is measured and arranged. Usually there is nothing in InitializeComponent() that causes a measure, so when it returns these will still be zero.

您可以通过在窗口的 InitializeComponent() 之后手动调用窗口的 Measure()Arrange() 方法来强制提前计算这些值代码>返回.

You can force these to be computed earlier by simply calling the window's Measure() and Arrange() methods manually after the window's InitializeComponent() returns.

如果您根据内容调整大小:

If you are sizing to content:

window.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
window.Arrange(new Rect(0, 0, window.DesiredSize.Width, window.DesiredSize.Height));

如果您使用的是明确的窗口大小:

If you are using an explicit window size:

window.Measure(new Size(Width, Height));
window.Arrange(new Rect(0, 0, window.DesiredSize.Width, window.DesiredSize.Height));

这篇关于在这种情况下,为什么 ActualWidth 和 ActualHeight 是 0.0?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆