使用触发器将按钮 IsEnabled 设置为鼠标悬停时为 true
我正在尝试设置一个按钮,因此只有当鼠标经过它时 按钮会将其视觉属性更改为启用按钮。
这是我的代码:
<Button Foreground="Black" Content="OK" Margin="186,100,170,172" >
<Button.Style>
<Style TargetType="Button">
<Setter Property="IsEnabled" Value="False" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="IsEnabled" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
提前感谢您的帮助。
I am trying to set a button so only when a mouse is going over it the
button will change his visual properties to an enabled button.
This is my code:
<Button Foreground="Black" Content="OK" Margin="186,100,170,172" >
<Button.Style>
<Style TargetType="Button">
<Setter Property="IsEnabled" Value="False" />
<Style.Triggers>
<Trigger Property="IsMouseOver" Value="True">
<Setter Property="IsEnabled" Value="True" />
</Trigger>
</Style.Triggers>
</Style>
</Button.Style>
</Button>
Thanks in advance for your help.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以将 Button 封装在 Border 中以获得您想要的东西
You could encapsulate your Button in a Border to get what you're after
我认为您不可能在这里执行此操作,因为当按钮被禁用时,IsMouseOver 为 false(而且,它不会生成 MouseMove 或其他事件)。
为了实现你想要的(顺便说一句,为什么当你将鼠标放在一个禁用的按钮上时,你会想要启用它?这太不自然了......如果你想用键盘导航到按钮怎么样?这是不可能的如果它被禁用),您应该重新设置按钮的样式,并使其在鼠标未位于其上方时看起来处于禁用状态,而当鼠标位于其上方时看起来处于启用状态 - 但这需要一些工作)。
I don't think it's possible what you want to do here, because while the button is disabled, IsMouseOver is false (also, it doesn't generate MouseMove or other events).
To achieve what you want (btw, why would you want a disabled button to become enabled when you put the mouse over it? It's so unnatural... how about if you want to navigate with the keyboard to the button? it will be impossible if it is disabled), you should restyle the button and make it look as in its disabled state when the mouse is not over it and as in its enabled state when the mouse is over it - but this will require some work).