36

How can I change an image when I hover over it?

All I have so far is:

<Image Height="32" Source="/images/Save32.png" />
TylerH
  • 20,799
  • 66
  • 75
  • 101
Eli Perpinyal
  • 1,706
  • 3
  • 19
  • 35

2 Answers2

86

You need to use a Trigger on the IsMouseOver property to modify the Source of the Image:

<Image>
  <Image.Style>
    <Style TargetType="{x:Type Image}">
      <Setter Property="Source" Value="C:\Image1.jpg"/>
      <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
          <Setter Property="Source" Value="C:\Image2.jpg"/>
        </Trigger>
      </Style.Triggers>
    </Style>
  </Image.Style>
</Image>

Note that Triggers can only be used inside Styles, and in order for a Trigger to change a property that property's value must be set by the Style and not set explicitly on the element.

Phil Devaney
  • 17,607
  • 6
  • 41
  • 33
  • 4
    Note that Triggers can only be used inside Styles, and in order for a Trigger to change a property that property's value must be set by the Style and not set explicitly on the element. - That just made a few problems I was having QUITE clear thanks! – Dave Jellison Feb 11 '11 at 21:07
8
<Image Stretch="Fill" >
        <Image.Style>
            <Style>
                <Setter Property="Image.Source" Value="original.png" />
                <Style.Triggers>
                    <Trigger  Property="Image.IsMouseOver" Value="True">
                        <Setter Property="Image.Source" Value="mouseover.png" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Image.Style>
</Image>

There are other ways that trigger. All right?

Oren Hizkiya
  • 4,420
  • 2
  • 23
  • 33
Vincent BOUZON
  • 177
  • 1
  • 7