如何在 WPF 中悬停时更改图像? [英] How do I change an image on hover over in WPF?

查看:23
本文介绍了如何在 WPF 中悬停时更改图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我将鼠标悬停在图像上时如何更改图像?

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

到目前为止,我只有:

<Image Height="32" Source="/images/Save32.png" />

推荐答案

需要在 IsMouseOver 属性上使用 Trigger 来修改 Image 的 Source:

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.

这篇关于如何在 WPF 中悬停时更改图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆