【问题标题】:How to add trigger for MouseUp in WPF?如何在 WPF 中为 MouseUp 添加触发器?
【发布时间】:2013-01-06 16:25:56
【问题描述】:

我想让我的按钮(我为Button 定义一个ControlTemplate)在鼠标向上(单击按钮并离开鼠标按钮)时触发我的动画。那是因为Button 在点击时没有VisualStateMouseUp,而只有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>

谢谢!

【问题讨论】:

    标签: wpf silverlight triggers wpf-controls controltemplate


    【解决方案1】:

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

    但是,使用 System.Windows.Interactivity 中的 EventTrigger,您似乎可以使用其他事件,例如 ClickMouseLeftButtonUp。示例见这篇文章:codeproject.com

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-03-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-23
      相关资源
      最近更新 更多