[英]WPF change Image when mouse hover on button
I'm trying to change the image when mouse is over a button, but it's not working here is what am trying to do :当鼠标悬停在按钮上时,我正在尝试更改图像,但在这里不起作用,这正是我想要做的:
<Button x:Name="Play" Content="" ClickMode="Press" BorderThickness="0" UseLayoutRounding="True" Height="120" Width="224">
<Button.Background>
<ImageBrush ImageSource="Resources/Play 1.gif"/>
</Button.Background>
<Button.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="ImageBrush.ImageSource" Value="Resources/Play 2.gif"></Setter>
</Trigger>
</Button.Triggers>
</Button>
But this gives me this error : Triggers collection members must be of type EventTrigger.
但这给了我这个错误:
Triggers collection members must be of type EventTrigger.
How would I make it work?我将如何使它工作?
It's correct - you can only put EventTriggers in there.这是正确的 - 您只能将 EventTriggers 放在那里。 You need to move your Trigger into the button's Style:
您需要将触发器移动到按钮的样式中:
<Button x:Name="Play" Content="" ClickMode="Press" BorderThickness="0" UseLayoutRounding="True" Height="120" Width="224">
<Button.Style>
<Style TargetType="{x:Type Button}">
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="..\Resources\Play 1.gif"/>
</Setter.Value>
</Setter>
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="Background">
<Setter.Value>
<ImageBrush ImageSource="..\Resources\Play 2.gif"/>
</Setter.Value>
</Setter>
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
Something like that, I think (haven't tested it).类似的东西,我想(还没有测试过)。
EDIT: This switches between the correct images, but it doesn't work correctly (it starts off with Play 1.gif, transitions to Play 2.gif, but then transitions to the button's underlying colour).编辑:这在正确的图像之间切换,但它不能正常工作(它从播放 1.gif 开始,过渡到播放 2.gif,然后过渡到按钮的底层颜色)。 I think you'll have to override the button's
ControlTemplate
to prevent this from happening, as suggested in the comments.我认为您必须按照评论中的建议覆盖按钮的
ControlTemplate
以防止这种情况发生。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.