tags:

views:

287

answers:

3

If I attach a callback to the loaded event of my main user control and check the 'ActualWidth' of a grid column, that value is still 0. I find it strange, given the fact that the documentation states: "The Loaded event is raised before the final rendering, but after the layout system has calculated all necessary values for rendering."

Basically I need the 'ActualWidth' so I can set the 'to' value of an animation (so something animates from a width of 0 to the actual width).

+1  A: 

I think you're probably trying to solve the problem in the wrong way.

Your best bet is to animate a ScaleTransform that's added to the Grids RenderTransform property. Here's an example that does just that:

   <Grid x:Name="LayoutRoot">
        <Grid.Triggers>
            <EventTrigger RoutedEvent="Grid.Loaded">
                <EventTrigger.Actions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetName="MyScaleTransform" Storyboard.TargetProperty="ScaleX" From="0" To="1" />
                        </Storyboard>
                    </BeginStoryboard>
                </EventTrigger.Actions>
            </EventTrigger>
        </Grid.Triggers>

        <Grid Background="Blue" >
            <Grid.RenderTransform>
                <ScaleTransform x:Name="MyScaleTransform"  />
            </Grid.RenderTransform>
        </Grid>
    </Grid>
James Hay
Thx for the reply, but it's not quite what I'm looking for. This solution has two drawbacks:- The first column obviously scales into existence rather than sliding in.- The content of the first column animates, but not the column itself.I think my solution isn't fundamentally flawed, but rather gets down to one problem/question: when can I expect the 'ActualWidth' property of the first column to be correct.
crimson13
+2  A: 

The ActualWidth and ActualHeight values are available during the SizeChanged event. Which works out nice, so you can update your calculations when your applications resizes if needed.

I posted about this recently using the event to randomly postion elements on the screen

Adam Kinney
+1  A: 

Can you hook or override the LayoutUpdated event in your control, rather than the Loaded event? You'd have to add some additional logic to make sure that your animation only ran once, at the appropriate time, but it might give you the additional flexibility you need.

Ken Smith
Never mind -- Adam Kinney's suggestion to use the SizeChanged event is better.
Ken Smith