动画图像按钮

发布于 2024-12-13 07:16:58 字数 1585 浏览 3 评论 0原文

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

  1. 正常到 MouseOver
  2. 、MouseOver 到按下

我对 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>

任何答案都会受到赞赏。

I need help with an custom "Image-Button" that i have used for a while. It works great, but i can't figure out how to animate the button with these three states (normal, mouseover, pressed):

  1. Normal to MouseOver
  2. MouseOver to Pressed

Im not that skilled with XAML so I couldn't figure it out. Anyway here's the code i've been using:

<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>

Any answer is appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

时光无声 2024-12-20 07:16:58

完成动画的另一种方法是使用触发器,就像结合 故事板

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

 <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>

An other way to accomplish animation is to use triggers like you did in combination with Storyboards

Here is some sample code (integrated in your posted code):

 <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>

客…行舟 2024-12-20 07:16:58

You should probably use the VisualStateManager for that sort of thing, see its documentation for a usage example.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文