【问题标题】:Animated Image-Button动画图像按钮
【发布时间】:2011-10-31 13:56:29
【问题描述】:

我需要有关我已经使用了一段时间的自定义“图像按钮”的帮助。它工作得很好,但我不知道如何用这三种状态(正常、鼠标悬停、按下)为按钮设置动画:

  1. MouseOver 正常
  2. 鼠标悬停到按下

我对 XAML 不太熟练,所以我想不通。无论如何,这是我一直在使用的代码:

<Button Height="40" HorizontalAlignment="Left" IsEnabled="True" IsHitTestVisible="True" Margin="262,219,0,0" Name="home_btn" VerticalAlignment="Top" Width="89">
    <Button.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <Grid>
                <Image Name="Normal" Source="normal.png" />
                <Image Name="Hover" Source="hover.png" Visibility="Hidden" />
                <Image Name="Pressed" Source="pressed.png" Visibility="Hidden" />
            </Grid>
            <ControlTemplate.Triggers>
                <Trigger Property="ButtonBase.IsPressed" Value="True">
                    <Setter Property="UIElement.Visibility" TargetName="Normal" Value="Hidden" />
                    <Setter Property="UIElement.Visibility" TargetName="Pressed" Value="Visible" />
                </Trigger>
                <Trigger Property="UIElement.IsMouseOver" Value="True">
                    <Setter Property="UIElement.Visibility" TargetName="Normal" Value="Hidden" />
                    <Setter Property="UIElement.Visibility" TargetName="Hover" Value="Visible" />
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Button.Template>
</Button>

感谢任何答案。

【问题讨论】:

    标签: wpf xaml animation imagebutton


    【解决方案1】:

    另一种实现动画的方法是使用触发器,就像您结合Storyboards 所做的那样

    这是一些示例代码(集成在您发布的代码中):

     <Button Height="40" HorizontalAlignment="Left" IsEnabled="True" IsHitTestVisible="True" Margin="262,219,0,0" Name="home_btn" VerticalAlignment="Top" Width="89">
    <Button.Template>
        <ControlTemplate TargetType="{x:Type Button}">
            <Grid>
                <Image Name="Normal" Source="normal.png" />
                <Image Name="Hover" Source="hover.png" Opacity="0"/>
                <Image Name="Pressed" Source="pressed.png" Opacity="0" />
            </Grid>
            <ControlTemplate.Resources>
                <Storyboard x:Key="MouseDownTimeLine">
                    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Pressed" Storyboard.TargetProperty="Opacity">
                        <SplineDoubleKeyFrame KeyTime="00:00:00.05" Value="1"/>
                    </DoubleAnimationUsingKeyFrames>
                </Storyboard>
                <Storyboard x:Key="MouseUpTimeLine">
                    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Pressed" Storyboard.TargetProperty="Opacity">
                        <SplineDoubleKeyFrame KeyTime="00:00:00.25" Value="0"/>
                    </DoubleAnimationUsingKeyFrames>
                </Storyboard>
                <Storyboard x:Key="MouseEnterTimeLine">
                    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Hover" Storyboard.TargetProperty="Opacity">
                        <SplineDoubleKeyFrame KeyTime="00:00:00.25" Value="1"/>
                    </DoubleAnimationUsingKeyFrames>
                </Storyboard>
                <Storyboard x:Key="MouseExitTimeLine">
                    <DoubleAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="Hover" Storyboard.TargetProperty="Opacity">
                        <SplineDoubleKeyFrame KeyTime="00:00:00.25" Value="0"/>
                    </DoubleAnimationUsingKeyFrames>
                 </Storyboard>
            </ControlTemplate.Resources>
            <ControlTemplate.Triggers>
                <Trigger Property="ButtonBase.IsPressed" Value="True">
                   <Trigger.EnterActions>
                       <BeginStoryboard Storyboard="{StaticResource MouseDownTimeLine}"/>
                   </Trigger.EnterActions>
                   <Trigger.ExitActions>
                       <BeginStoryboard Storyboard="{StaticResource MouseUpTimeLine}"/>
                   </Trigger.ExitActions>
                </Trigger>
                <Trigger Property="UIElement.IsMouseOver" Value="True">
                    <Trigger.EnterActions>
                        <BeginStoryboard Storyboard="{StaticResource MouseEnterTimeLine}"/>
                    </Trigger.EnterActions>
                    <Trigger.ExitActions>
                        <BeginStoryboard Storyboard="{StaticResource MouseExitTimeLine}"/>
                    </Trigger.ExitActions>
                </Trigger>
            </ControlTemplate.Triggers>
        </ControlTemplate>
    </Button.Template>
    

    【讨论】:

      【解决方案2】:

      您可能应该使用VisualStateManager 来处理这类事情,请参阅其文档以获取使用示例。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2013-12-25
        • 2017-05-11
        • 1970-01-01
        • 1970-01-01
        • 2020-08-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多