如何在 WPF 中悬停时更改图像?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1502914/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I change an image on hover over in WPF?
提问by Eli Perpinyal
How can I change an image when I hover over it?
将鼠标悬停在图像上时如何更改图像?
All I have so far is:
到目前为止我所拥有的是:
<Image Height="32" Source="/images/Save32.png" />
回答by Phil Devaney
You need to use a Trigger on the IsMouseOver property to modify the Source of the Image:
您需要在 IsMouseOver 属性上使用触发器来修改图像的来源:
<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.
请注意,触发器只能在样式内使用,并且为了让触发器更改属性,该属性的值必须由样式设置,而不是在元素上显式设置。
回答by Vincent BOUZON
<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?
还有其他触发方式。好的?