在这种情况下,为什么 ActualWidth 和 ActualHeight 为 0.0? [英] Why are ActualWidth and ActualHeight 0.0 in this case?
问题描述
我在 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?
观察:
- 我没有定义外部画布的宽度和高度,但如果我定义了,也不能解决我的问题.
- 在运行时我可以看到这个
Canvas/Grid
占据了整个窗口空间,所以它里面的每个矩形都有ActualWidth
s 和ActualHeight
- 网格的宽度/高度已绑定到画布,但我尝试删除此绑定,但问题仍然存在.
- I'm not defining the outer canvas width and height but if I do, it doesn't solve my problem.
- At runtime I can see that this
Canvas/Grid
occupies the entire window space, so every rectangle inside it hasActualWidth
s andActualHeight
s - The grid's width/height is bound to the canvas but I tried removing this binding and my problem still persists.
推荐答案
ActualHeight
和 ActualWidth
直到控件被测量和排列后才设置.通常 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屋!