1

这里有一个简单的矩形旋转动画示例:WPF Rotate rectangle animation in XAML

但是,我只想在鼠标悬停时旋转矩形,并在鼠标移出时停止。到目前为止,这是我的 xaml:

<Button Command="{Binding SettingsCommand}" Style="{DynamicResource SettingButton}">
    <Rectangle Width="15" Height="15" RenderTransformOrigin="0.5, 0.5">
        <Rectangle.RenderTransform>
            <RotateTransform/>
        </Rectangle.RenderTransform>
        <Rectangle.Triggers>
            <EventTrigger RoutedEvent="Loaded">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation Storyboard.TargetProperty="(Rectangle.RenderTransform).(RotateTransform.Angle)" To="-360" Duration="0:0:1" RepeatBehavior="Forever"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Rectangle.Triggers>
        <Rectangle.Fill>
            <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_cog}"/>
        </Rectangle.Fill>
    </Rectangle>
</Button>

我怎样才能修改它只在鼠标悬停时工作?

谢谢!

4

1 回答 1

1

MouseEnter您可以对和MouseLeave事件使用触发器:

<Button Command="{Binding SettingsCommand}" Style="{DynamicResource SettingButton}">
    <Rectangle Width="15" Height="15" RenderTransformOrigin="0.5, 0.5">
        <Rectangle.RenderTransform>
            <RotateTransform/>
        </Rectangle.RenderTransform>
        <Rectangle.Triggers>
            <EventTrigger RoutedEvent="MouseEnter">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation
                            Storyboard.TargetProperty="RenderTransform.Angle"
                            To="-360" Duration="0:0:1" RepeatBehavior="Forever"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
            <EventTrigger RoutedEvent="MouseLeave">
                <BeginStoryboard>
                    <Storyboard>
                        <DoubleAnimation
                            Storyboard.TargetProperty="RenderTransform.Angle"
                            To="0" Duration="0:0:0"/>
                    </Storyboard>
                </BeginStoryboard>
            </EventTrigger>
        </Rectangle.Triggers>
        <Rectangle.Fill>
            <VisualBrush Stretch="Fill" Visual="{StaticResource appbar_cog}"/>
        </Rectangle.Fill>
    </Rectangle>
</Button>
于 2013-08-09T15:36:38.553 回答