I use Styles to define what happens when a user hovers over an image like this.
<Style TargetType="{x:Type Image}">
<Setter Property="Source" Value="c:\\2.bmp"/>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Source" Value="C:\\1.bmp"/>
</Trigger>
</Style.Triggers>
</Style>
But, I guess for all the images that I include in my application, the same images defined in ‘Value’ attributes are used. What If I need a different on-hover image for different Images without this custom overriding?
In this case, any ideas what I should use? Or what is the better way to handle hovering of images in WPF?
You can define several different styles and give them an
x:Keyvalue, rather than aTargetTypevalue. You can then simply set theStyleattribute of your images to thex:Keyof the appropriate style.If you want to apply a single style to the majority of your Images, but you want to override it for just a few of them, then you can combine the two methods. If you assign a style to a type of element (via the
TargetTypeattribute), you can override this on specific elements by explicitly specifying theirStyleattribute. Also, if you setStyle={x:Null}, you can make an element revert to the default style.