开发者

WPF double animation set wrong value

I have simple code to animate movement on canvas. The problem is that after animation complete开发者_如何学JAVAd element does not have proper value.

Here is example:

<Canvas Name="_canvas" >
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Name="button1" VerticalAlignment="Top" Width="292"  Canvas.Left="56" Canvas.Top="26">
    <Button.Triggers>
            <EventTrigger RoutedEvent="ButtonBase.Click">
                <BeginStoryboard>
                    <Storyboard Completed="Timeline_OnCompleted">
                        <DoubleAnimation Duration="0:0:1" To="264" Storyboard.TargetProperty="(Canvas.Top)"/>                       
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Button.Triggers>
    </Button>       
</Canvas>

And in code behind

private void Timeline_OnCompleted(object sender, EventArgs e)
{
    button1.Content = "Top is " + Canvas.GetTop(button1) + "and should be 264";
}

Result after complete an animation is

WPF double animation set wrong value


This is a confirmed bug in WPF: The storyboard's Completed event fires before the contained timelines are completed (see Connect).

If you handle the Completed event on your DoubleAnimation instead, it will show the correct end value.


Seems like OnCompleted is called before the last animation step is done. If you really need to read button's Top position (i.e. getting DoubleAnimation.To is not acceptable), try running Canvas.GetTop(button1) when application will be idle (or try other priorities of dispatcher).

Dispatcher.BeginInvoke(
    (Action)(
        () => button1.Content = "Top is " + Canvas.GetTop(button1) + "and should be 264"
    ),
    DispatcherPriority.ApplicationIdle
);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜