wpf 在这种情况下,为什么 ActualWidth 和 ActualHeight 为 0.0?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1695101/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why are ActualWidth and ActualHeight 0.0 in this case?
提问by André Pena
I have a Grid
inside a Canvas
defined like this:
我有一个像这样定义的Grid
内部Canvas
:
<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>
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?
我的问题是,在 Window 构造函数中,在InitializeComponents()
任何一个Grid.ColumnDefinitions[0].ActualWidth
或“任何矩形”之后。ActualWidth
都设置为 0.0(高度相同)。我不知道该怎么做才能获得这些信息。有什么帮助吗?
Observations:
观察:
- 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.
- 我没有定义外部画布的宽度和高度,但如果我这样做了,它并不能解决我的问题。
- 在运行时我可以看到它
Canvas/Grid
占据了整个窗口空间,所以它里面的每个矩形都有ActualWidth
s 和ActualHeight
s - 网格的宽度/高度绑定到画布,但我尝试删除此绑定,但我的问题仍然存在。
回答by Ray Burns
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.
ActualHeight
并且ActualWidth
在控制被测量和安排之前不会被设置。通常没有什么InitializeComponent()
会导致度量,所以当它返回时,这些仍然是零。
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.
您可以通过在窗口返回后手动调用窗口Measure()
和Arrange()
方法来强制提前计算这些InitializeComponent()
值。
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));
回答by Kent Boogaart
Ray is correct (+1) that this is due to the fact that the measure and arrange pass has not executed yet. However, rather than force another layout pass (expensive), you can just wait until your control has loaded before accessing the ActualXxx
properties:
Ray 是正确的 (+1),这是因为尚未执行测量和安排传递。但是,与其强制进行另一次布局传递(昂贵),您可以在访问ActualXxx
属性之前等待控件加载完毕:
public MyWindow()
{
Loaded += delegate
{
// access ActualWidth and ActualHeight here
};
}
回答by Gabriel
In our case the solution was simple, as everybody said ActualWidth and ActualHeight needed to get called after the Loaded even completes, So we just wrapped the code in a dispatcher and set the priority to Loaded as below:
在我们的例子中,解决方案很简单,因为每个人都说在 Loaded 完成后需要调用 ActualWidth 和 ActualHeight,所以我们只是将代码包装在调度程序中并将优先级设置为 Loaded,如下所示:
Dispatcher.Invoke(new Action(() =>
{
graphHeight = ActualHeight;
graphWidth = ActualWidth;
}), DispatcherPriority.Loaded);
回答by Rahul Saksule
ActualWidth
and ActualHeight
are available only after XAML loading completed,before loading how can you find these parameters?
ActualWidth
并且ActualHeight
只有在XAML加载完成后才可用,加载之前如何找到这些参数?
Use Loaded event and inside it do find the ActualWidth
and ActualHeight
.
使用 Loaded 事件并在其中找到ActualWidth
and ActualHeight
。
private void me_Loaded(object sender, RoutedEventArgs e)
{
// do your stuffs here.
}