三态图像按钮无法同时将鼠标悬停在上面和压在一起工作
您好,我正在尝试使用三个图像创建一个三状态按钮。
我无法让鼠标悬停和按下的触发器同时工作。在下面的代码中,鼠标悬停工作正常,但按下按钮时不会显示按下的图像。
<Button x:Name="button"/>
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Image x:Name="Normal" Source="run.png" Visibility="Visible"/>
<Image x:Name="Pressed" Source="pressed.png" Visibility="Hidden"/>
<Image x:Name="Over" Source="over.png" Visibility="Hidden"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="Pressed" Property="Visibility" Value="Visible"/>
<Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
<Setter TargetName="Over" Property="Visibility" Value="Hidden"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Over" Property="Visibility" Value="Visible"/>
<Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter TargetName="Over" Property="Visibility" Value="Hidden"/>
<Setter TargetName="Normal" Property="Visibility" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate></Button.Template></Button>
有什么想法吗?
Hi I'm trying to create a three state button using three images.
I cannot get both mouse over and pressed triggers work together. In the code below, mouse over works fine but the pressed image does not show when the button is pressed.
<Button x:Name="button"/>
<Button.Template>
<ControlTemplate TargetType="{x:Type Button}">
<Grid>
<Image x:Name="Normal" Source="run.png" Visibility="Visible"/>
<Image x:Name="Pressed" Source="pressed.png" Visibility="Hidden"/>
<Image x:Name="Over" Source="over.png" Visibility="Hidden"/>
</Grid>
<ControlTemplate.Triggers>
<Trigger Property="IsPressed" Value="True">
<Setter TargetName="Pressed" Property="Visibility" Value="Visible"/>
<Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
<Setter TargetName="Over" Property="Visibility" Value="Hidden"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="True">
<Setter TargetName="Over" Property="Visibility" Value="Visible"/>
<Setter TargetName="Normal" Property="Visibility" Value="Hidden"/>
</Trigger>
<Trigger Property="IsMouseOver" Value="False">
<Setter TargetName="Over" Property="Visibility" Value="Hidden"/>
<Setter TargetName="Normal" Property="Visibility" Value="Visible"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate></Button.Template></Button>
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
触发器按照它们在 XAML 中出现的顺序进行评估,因此我认为您很可能只需要按如下方式重新排序:
这样
IsPressed
设置将覆盖IsMouseOver
等价物。Triggers are evaluated in the order they appear in the XAML, so I think you most probably just need to re-order them as below:
This way the
IsPressed
settings will override theIsMouseOver
equivalents.