触发动画

时间:2009-06-28 14:11:44

标签: wpf xaml animation triggers storyboard

我有一个usercontrol,当我双击它时,我希望它放大,如果它还没有。如果是,则双击将缩小它。我可以使用后面的代码,但我不能让它在xaml中工作。 以下是该句柄背后的代码是双击事件。

void MyObjectMouseDoubleClick(object sender, MouseButtonEventArgs e)
    {
        if (IsZoomedIn)
        {
            IsZoomedIn = false;
            //ZoomOutAnimation();
        }
        else
        {
            IsZoomedIn = true;
            //ZoomInAnimation();
        }
    }

然后在我的xaml中:

<UserControl.RenderTransform>
    <TransformGroup>
        <RotateTransform />
        <ScaleTransform />
        <TranslateTransform />
    </TransformGroup>
</UserControl.RenderTransform>

<UserControl.Style>
    <Style>
        <Style.Triggers>
            <Trigger Property="local:MyObject.IsZoomedIn" Value="False">
                <Trigger.EnterActions>
                    <BeginStoryboard>
                        <Storyboard>
                            <DoubleAnimation Storyboard.TargetProperty="(UserControl.
                            RenderTransform).(TransformGroup.Children)[1].
                            (ScaleTransform.ScaleX)" To="1" Duration="0:0:.3" />
                            <DoubleAnimation Storyboard.TargetProperty="(UserControl.
                            RenderTransform).(TransformGroup.Children)[1].
                            (ScaleTransform.ScaleY)" To="1" Duration="0:0:.3" />
                        </Storyboard>
                    </BeginStoryboard>
                </Trigger.EnterActions>
            </Trigger>
            <Trigger Property="local:MyObject.IsZoomedIn" Value="True">
                <Trigger.EnterActions>        
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetProperty="(UserControl.
                        RenderTransform).(TransformGroup.Children)[1].
                        (ScaleTransform.ScaleX)" To="2" Duration="0:0:.3" />
                        <DoubleAnimation Storyboard.TargetProperty="(UserControl.
                        RenderTransform).(TransformGroup.Children)[1].
                        (ScaleTransform.ScaleY)" To="2" Duration="0:0:.3" />
                    </Storyboard>
                </BeginStoryboard>
                </Trigger.EnterActions>
            </Trigger>
        </Style.Triggers>
    </Style>
</UserControl.Style>

一旦放大我的usercontrol,缩小动画就不起作用了。任何帮助赞赏。
感谢。

1 个答案:

答案 0 :(得分:4)

您的动画正在保留值,因此即使触发器正在触发,第二个动画也不会出现。您可以像使用EnterActions一样使用Trigger.ExitActions,而不是使用两个单独的触发器。

<Trigger Property="IsZoomedIn"
         Value="True">
    <Trigger.EnterActions>
        <BeginStoryboard>
            <Storyboard >
                <DoubleAnimation Storyboard.TargetProperty="(UserControl.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleX)"     
                                 To="2"
                                 Duration="0:0:.3" />
                <DoubleAnimation Storyboard.TargetProperty="(UserControl.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleY)"
                                 To="2"
                                 Duration="0:0:.3" />
            </Storyboard>
        </BeginStoryboard>
    </Trigger.EnterActions>
    <Trigger.ExitActions>
        <BeginStoryboard>
            <Storyboard>
                <DoubleAnimation Storyboard.TargetProperty="(UserControl.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleX)"
                                 To="1"
                                 Duration="0:0:.3" />
                <DoubleAnimation Storyboard.TargetProperty="(UserControl.RenderTransform).(TransformGroup.Children)[1].(ScaleTransform.ScaleY)"
                                 To="1"
                                 Duration="0:0:.3" />
            </Storyboard>
        </BeginStoryboard>
    </Trigger.ExitActions>
</Trigger>