如何反转WPF故事板动画?

时间:2022-11-06 00:15:56

I've created a WPF Storyboard animation on an image in Expression Blend 4. On hover, the image gradually blurs. Is there any way I can have the Storyboard be undone or reversed when the mouse leaves the image? I could make it trigger Storyboard.Remove() but that wouldn't actually play through the Storyboard backwards.

我在Expression Blend 4中的一个图像上创建了一个WPF故事板动画。在盘旋的时候,图像逐渐模糊。当鼠标离开图像时,是否有办法让故事板复原或反转?我可以让它触发Storyboard. remove(),但这并不能反过来通过故事板来实现。

Is there any way I can accomplish that within Expression Blend 4?

有什么方法可以在Expression Blend 4中实现吗?

2 个解决方案

#1


11  

Since you are using Blend, you should take advantage of Blend's support for the VisualStateManager. All you have to do is describe what the object looks like in its various states, like MouseOver and Normal and how long the transitions between various states are, and the visual state manager works out how to transition between states.

由于您正在使用Blend,所以您应该利用Blend对VisualStateManager的支持。您所要做的就是描述对象在其不同状态下的样子,比如MouseOver和Normal,以及不同状态之间的转换时间,视觉状态管理器会计算出如何在状态之间转换。

An image doesn't have any visual states but you can edit a Button template and make its content an image and then edit the states for the button. I've done this and cleaned up the XAML to demonstrate the technique:

图像没有任何视觉状态,但是您可以编辑一个按钮模板,使其内容为图像,然后编辑按钮的状态。我已经这样做了,并清理了XAML来演示技术:

<Grid>
    <Grid.Resources>
        <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Image x:Name="image" Height="100" Width="Auto" Source="http://thecybershadow.net/misc/*.png" Margin="0,0,-25,0">
                            <Image.Effect>
                                <DropShadowEffect ShadowDepth="0"/>
                            </Image.Effect>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualStateGroup.Transitions>
                                        <VisualTransition GeneratedDuration="0:0:0.5"/>
                                    </VisualStateGroup.Transitions>
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver">
                                        <Storyboard>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Effect).(DropShadowEffect.ShadowDepth)" Storyboard.TargetName="image">
                                                <EasingDoubleKeyFrame KeyTime="0" Value="15"/>
                                            </DoubleAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Pressed"/>
                                    <VisualState x:Name="Disabled"/>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                        </Image>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Grid.Resources>
    <Button Style="{StaticResource ButtonStyle1}"/>
</Grid>

Note that Blend does all this for you but understanding the XAML will help. Here's a Blend-oriented tutorial:

注意Blend为您做了所有这些,但是理解XAML将会有所帮助。这里有一个Blend-oriented教程:

#2


0  

Maybe the solution is on this Answer:

也许答案就在这里:

How to animate ListBox Items on MouseEnter and MouseLeave events using C#/WPF?

如何使用c# /WPF在MouseEnter和MouseLeave事件上激活ListBox项目?

#1


11  

Since you are using Blend, you should take advantage of Blend's support for the VisualStateManager. All you have to do is describe what the object looks like in its various states, like MouseOver and Normal and how long the transitions between various states are, and the visual state manager works out how to transition between states.

由于您正在使用Blend,所以您应该利用Blend对VisualStateManager的支持。您所要做的就是描述对象在其不同状态下的样子,比如MouseOver和Normal,以及不同状态之间的转换时间,视觉状态管理器会计算出如何在状态之间转换。

An image doesn't have any visual states but you can edit a Button template and make its content an image and then edit the states for the button. I've done this and cleaned up the XAML to demonstrate the technique:

图像没有任何视觉状态,但是您可以编辑一个按钮模板,使其内容为图像,然后编辑按钮的状态。我已经这样做了,并清理了XAML来演示技术:

<Grid>
    <Grid.Resources>
        <Style x:Key="ButtonStyle1" TargetType="{x:Type Button}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type Button}">
                        <Image x:Name="image" Height="100" Width="Auto" Source="http://thecybershadow.net/misc/*.png" Margin="0,0,-25,0">
                            <Image.Effect>
                                <DropShadowEffect ShadowDepth="0"/>
                            </Image.Effect>
                            <VisualStateManager.VisualStateGroups>
                                <VisualStateGroup x:Name="CommonStates">
                                    <VisualStateGroup.Transitions>
                                        <VisualTransition GeneratedDuration="0:0:0.5"/>
                                    </VisualStateGroup.Transitions>
                                    <VisualState x:Name="Normal"/>
                                    <VisualState x:Name="MouseOver">
                                        <Storyboard>
                                            <DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Effect).(DropShadowEffect.ShadowDepth)" Storyboard.TargetName="image">
                                                <EasingDoubleKeyFrame KeyTime="0" Value="15"/>
                                            </DoubleAnimationUsingKeyFrames>
                                        </Storyboard>
                                    </VisualState>
                                    <VisualState x:Name="Pressed"/>
                                    <VisualState x:Name="Disabled"/>
                                </VisualStateGroup>
                            </VisualStateManager.VisualStateGroups>
                        </Image>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Grid.Resources>
    <Button Style="{StaticResource ButtonStyle1}"/>
</Grid>

Note that Blend does all this for you but understanding the XAML will help. Here's a Blend-oriented tutorial:

注意Blend为您做了所有这些,但是理解XAML将会有所帮助。这里有一个Blend-oriented教程:

#2


0  

Maybe the solution is on this Answer:

也许答案就在这里:

How to animate ListBox Items on MouseEnter and MouseLeave events using C#/WPF?

如何使用c# /WPF在MouseEnter和MouseLeave事件上激活ListBox项目?