Jonas Libbe
Jonas Libbe

Reputation: 1812

Animated Image-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.

Upvotes: 1

Views: 3829

Answers (2)

dasheddot
dasheddot

Reputation: 2966

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>

Upvotes: 4

brunnerh
brunnerh

Reputation: 185553

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

Upvotes: 1

Related Questions