开发者

Silverlight: when is ActualWidth not 0?

If I attach a callback to the loaded event of my main user control and check the ActualWidth of a grid column, that value开发者_如何学C 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).


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


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>


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜