如何在WPF中添加MouseUp的触发器?

时间:2013-01-06 16:25:56

标签: wpf silverlight triggers wpf-controls controltemplate

我想制作我的按钮(我为ControlTemplate定义Button)以在鼠标按下时触发我的动画(单击按钮并按住鼠标按钮)。这是因为Button在点击时没有VisualState MouseUp而只有Pressed

我希望在没有按住按钮的情况下执行动画,我只想点击并释放。

这是我的代码(你可以看到我放了但是它没有产生任何影响:

<Window.Resources>
    <Style x:Key="ButtonStyle1" TargetType="Button">
        <Setter Property="HorizontalAlignment" Value="Stretch" />
        <Setter Property="VerticalAlignment" Value="Stretch" />
        <Setter Property="HorizontalContentAlignment" Value="Center" />
        <Setter Property="VerticalContentAlignment" Value="Center" />
        <Setter Property="BorderThickness" Value="1" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="Button">
                    <Border x:Name="BorderOfTemplate" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" RenderTransformOrigin=".5,.5">
                        <Border.Triggers>
                            <EventTrigger RoutedEvent="Click">
                                <BeginStoryboard>
                                    <Storyboard>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetName="AnimatedScaleTransform" Storyboard.TargetProperty="ScaleX" Duration="0:0:3" RepeatBehavior="1x">
                                            <LinearDoubleKeyFrame KeyTime="0:0:3" Value=".1" />
                                            <SplineDoubleKeyFrame  Value="1" KeyTime="0:0:3" />
                                        </DoubleAnimationUsingKeyFrames>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetName="AnimatedScaleTransform" Storyboard.TargetProperty="ScaleY" Duration="0:0:3" RepeatBehavior="1x">
                                            <LinearDoubleKeyFrame KeyTime="0:0:3" Value=".1" />
                                            <SplineDoubleKeyFrame  Value="1" KeyTime="0:0:3" />
                                        </DoubleAnimationUsingKeyFrames>
                                        <DoubleAnimationUsingKeyFrames Storyboard.TargetName="AnimatedTranslateTransform" Storyboard.TargetProperty="Y" Duration="0:0:3" RepeatBehavior="1x">
                                            <LinearDoubleKeyFrame Value="-170" KeyTime="0:0:2" />
                                            <SplineDoubleKeyFrame  Value="1" KeyTime="0:0:3" />
                                        </DoubleAnimationUsingKeyFrames>
                                    </Storyboard>
                                </BeginStoryboard>
                            </EventTrigger>
                        </Border.Triggers>

                        <Border.RenderTransform>
                            <TransformGroup>
                                <RotateTransform x:Name="AnimatedRotateTransform" />
                                <ScaleTransform x:Name="AnimatedScaleTransform" />
                                <TranslateTransform x:Name="AnimatedTranslateTransform" />
                            </TransformGroup>
                        </Border.RenderTransform>

                     <here some VisualStateManager>
                     </here some VisualStateManager>
enter code here
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
</Window.Resources>

谢谢!

1 个答案:

答案 0 :(得分:0)

根据this article,Silverlight的普通EventTrigger仅支持Loaded事件。

但是,使用EventTrigger中的System.Windows.Interactivity,您似乎可以使用其他活动,例如点击 MouseLeftButtonUp 。请参阅此文章以获取示例:codeproject.com