WPF的Image在XAML中如何实现鼠标划过时改变图片?

一树秋叶 2016-10-14 12:53:51
后台代码我知道怎么改变,但是考虑单单改变图片不应该是视觉方面的工作吗,就想在XAML里实现一下。

下面是代码

<Image.Triggers>
<Trigger Property="Image.IsMouseOver" Value="true">
<Setter TargetName="imageOneKey" Property="Source" Value="/PunchControl;component/Resources/onekey_.png"></Setter>
</Trigger>
</Image.Triggers>


会报错提示“Source”成员无效,因为它不具有限定的类型名。
...全文
516 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
一树秋叶 2016-10-14
  • 打赏
  • 举报
回复
引用 7 楼 xinweilee 的回复:
貌似msdn解释FrameworkElement.Triggers只支持eventtrigger https://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.triggers(v=vs.110).aspx
谢谢,果然是这个意思,里面说了属性触发器必须用在style或者template中
xinweilee 2016-10-14
  • 打赏
  • 举报
回复
貌似msdn解释FrameworkElement.Triggers只支持eventtrigger https://msdn.microsoft.com/en-us/library/system.windows.frameworkelement.triggers(v=vs.110).aspx
一树秋叶 2016-10-14
  • 打赏
  • 举报
回复
引用 4 楼 duanzi_peng 的回复:
[quote=引用 2 楼 kuhaizhengzha 的回复:] [quote=引用 1 楼 duanzi_peng 的回复:] Source - 》 Image.Source 试试
在加载打开这个页面时报错: “初始化“System.Windows.Controls.Image”时引发了异常。”,行号为“17”,行位置为“19”。[/quote]

<Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Window.Resources>
  <!--替换背景img-->
  <BitmapImage x:Key="MyImageSource" UriSource="替换按钮.png" />
    <!--Image Style-->
    <Style x:Key="ImageStyle1" TargetType="{x:Type Image}">
			<Setter Property="FlowDirection" Value="LeftToRight"/>
			<!--原背景img-->
			<Setter Property="Source" Value="按钮.png"/>
			<Setter Property="OpacityMask" Value="{x:Null}"/>
			<Style.Triggers>
				<Trigger Property="IsMouseOver" Value="True">
					<Setter Property="Source" Value="{StaticResource MyImageSource}"></Setter>
				</Trigger>
			</Style.Triggers>
		</Style>
  </Window.Resources>
  <Grid>
     <Image  x:Name="img" Style="{StaticResource ImageStyle1}" Width="100" Height="200"></Image>
  </Grid>
</Window>
[/quote] 谢谢,和3楼是一个意思。我就是有个疑问,这个Source属性必须在Style里设置,然后在Style里用触发器才可以吗?如果是直接设置了一个默认图片,然后直接像主楼那样用了触发器是不可以的吗?
一树秋叶 2016-10-14
  • 打赏
  • 举报
回复
引用 3 楼 xinweilee 的回复:
<Image.Style> <Style TargetType="Image"> <Style.Setters> <Setter Property="Source" Value="/WpfApplication7;component/Resource/book.bmp"/> </Style.Setters> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Trigger.Setters> <Setter Property="Source" Value="/WpfApplication7;component/Resource/Koala.jpg"/> </Trigger.Setters> </Trigger> </Style.Triggers> </Style> </Image.Style> 这样是可以的
谢谢,这个是可以的。但是有个疑问,必须得设置了Style才可以吗?直接用Trigger触发器不行吗?
exception92 2016-10-14
  • 打赏
  • 举报
回复
引用 2 楼 kuhaizhengzha 的回复:
[quote=引用 1 楼 duanzi_peng 的回复:] Source - 》 Image.Source 试试
在加载打开这个页面时报错: “初始化“System.Windows.Controls.Image”时引发了异常。”,行号为“17”,行位置为“19”。[/quote]

<Window
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Window.Resources>
  <!--替换背景img-->
  <BitmapImage x:Key="MyImageSource" UriSource="替换按钮.png" />
    <!--Image Style-->
    <Style x:Key="ImageStyle1" TargetType="{x:Type Image}">
			<Setter Property="FlowDirection" Value="LeftToRight"/>
			<!--原背景img-->
			<Setter Property="Source" Value="按钮.png"/>
			<Setter Property="OpacityMask" Value="{x:Null}"/>
			<Style.Triggers>
				<Trigger Property="IsMouseOver" Value="True">
					<Setter Property="Source" Value="{StaticResource MyImageSource}"></Setter>
				</Trigger>
			</Style.Triggers>
		</Style>
  </Window.Resources>
  <Grid>
     <Image  x:Name="img" Style="{StaticResource ImageStyle1}" Width="100" Height="200"></Image>
  </Grid>
</Window>
xinweilee 2016-10-14
  • 打赏
  • 举报
回复
<Image.Style> <Style TargetType="Image"> <Style.Setters> <Setter Property="Source" Value="/WpfApplication7;component/Resource/book.bmp"/> </Style.Setters> <Style.Triggers> <Trigger Property="IsMouseOver" Value="True"> <Trigger.Setters> <Setter Property="Source" Value="/WpfApplication7;component/Resource/Koala.jpg"/> </Trigger.Setters> </Trigger> </Style.Triggers> </Style> </Image.Style> 这样是可以的
一树秋叶 2016-10-14
  • 打赏
  • 举报
回复
引用 1 楼 duanzi_peng 的回复:
Source - 》 Image.Source 试试
在加载打开这个页面时报错: “初始化“System.Windows.Controls.Image”时引发了异常。”,行号为“17”,行位置为“19”。
exception92 2016-10-14
  • 打赏
  • 举报
回复
Source - 》 Image.Source 试试

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧