Why are ActualWidth and ActualHeight 0.0 in this case?
Asked Answered
E

3

49

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>

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?

Observations:

  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.
Evelineevelinn answered 8/11, 2009 at 2:4 Comment(0)
M
85

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.

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));
Machinegun answered 8/11, 2009 at 5:53 Comment(2)
Thanks! Just a little correction: window.Measure returns void. It changes the DesiredWidth/DesiredHeight properties :) But your idea is right.Evelineevelinn
That's what happens when you type code off the top of your head without double-checking the APIs. Thanks for the correction. MeasureOverride returns the size, but Measure just stores them. Answer corrected.Machinegun
J
72

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:

public MyWindow()
{
    Loaded += delegate
    {
        // access ActualWidth and ActualHeight here
    };

}
Judicative answered 8/11, 2009 at 9:4 Comment(6)
Agree. Waiting for Loaded is the better approach IMHO.Choiseul
Thanks! Just curious... Why there's no an OnLoaded method on Window?Evelineevelinn
Waiting for Loaded is a good idea (+1), and will work in most cases. But note that when you never connect to a PresentationSource (ie. you never show the window), Loaded will never be called. I've run into this before, for example when deciding whether or not to include a control on a page. So I would say using Loaded is better when it is possible and covers all cases, otherwise using Measure/Arrange is better. That said, in 90% of the cases best practice will be to stay away from ActualWidth/ActualHeight completely. (eg. more/beter panels, binding to height/width, MeasureOverride)Machinegun
Tried both, WAAAAAAYYYYYYYY better.Finch
@RayBurns, my loaded is not called, as I ran into the 10%. Is there a way to call it (beats re-writing in IMO).Sverre
Outstanding answer. Thanks for sharing!Cribb
B
3

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:

Dispatcher.Invoke(new Action(() =>
{
   graphHeight = ActualHeight;
   graphWidth = ActualWidth;
}), DispatcherPriority.Loaded);
Benisch answered 21/3, 2017 at 15:24 Comment(1)
this solutions do help me. I get the actual size from same thread and it return zero and NaN. I take this code and place it after the thread, and it show the results accordingly.Abrego

© 2022 - 2024 — McMap. All rights reserved.