views:

26

answers:

1

I've done this once before but I have forgotten entirely how to do it.

How can I have it so an Image can grow a bit within 3 seconds (so it's smooth) on MouseOver?

+3  A: 

One way is to handle the MouseEnter and MouseLeave events with triggers, and use them to scale the image up:

<Image Width="100" Height="100" Source="...">
    <Image.RenderTransform>
        <ScaleTransform 
            x:Name="scale" 
            CenterX="50" 
            CenterY="50" 
            ScaleX="1" 
            ScaleY="1" />
    </Image.RenderTransform>

    <Image.Triggers>
        <EventTrigger RoutedEvent="Image.MouseEnter">
            <BeginStoryboard>
                <BeginStoryboard.Storyboard>
                    <Storyboard Duration="0:0:0.2">
                        <DoubleAnimation 
                            Storyboard.TargetName="scale" 
                            Storyboard.TargetProperty="ScaleX" 
                            To="1.5" />
                        <DoubleAnimation 
                            Storyboard.TargetName="scale" 
                            Storyboard.TargetProperty="ScaleY" 
                            To="1.5" />
                    </Storyboard>
                </BeginStoryboard.Storyboard>
            </BeginStoryboard>
        </EventTrigger>

        <EventTrigger RoutedEvent="Image.MouseLeave">
            <BeginStoryboard>
                <BeginStoryboard.Storyboard>
                    <Storyboard Duration="0:0:0.2">
                        <DoubleAnimation 
                            Storyboard.TargetName="scale" 
                            Storyboard.TargetProperty="ScaleX" 
                            To="1" />
                        <DoubleAnimation 
                            Storyboard.TargetName="scale" 
                            Storyboard.TargetProperty="ScaleY" 
                            To="1" />
                    </Storyboard>
                </BeginStoryboard.Storyboard>
            </BeginStoryboard>
        </EventTrigger>
    </Image.Triggers>
</Image>
Matt Hamilton
This works wonderfully, but the image grows and then doesn't shrink back to it's original state. Can you help me with this little bug?
Sergio Tapia
It should - it did in my test (from which I copied this code). The "MouseLeave" event handler with the storyboard reverting the scale back to 1x1 should handle that.
Matt Hamilton
Do you think that maybe the code resizes it to 1x1 of the original image size and NOT of the control size? Becuase my control is set smaller than the actual image dimensions. Any guidance?
Sergio Tapia
Have you set an explicit size on your Image, as I have here? It might be stretching to fill its parent container.
Matt Hamilton