简体   繁体   English

WPF按钮对我来说正确的触发器是什么?

[英]WPF Button what is the right Trigger for me?

I've been trying several properties but I can't seem to get the result I want. 我一直在尝试几个属性,但似乎无法获得想要的结果。 Currently, this is what's in my resource dictionary: 当前,这是我的资源字典中的内容:

<Trigger Property="IsMouseOver" Value="True">
    <Setter TargetName="marker" Property="Fill" Value="Black" />
</Trigger>
<Trigger Property="IsFocused" Value="True">
    <Setter TargetName="marker" Property="Fill" Value="Black" />
</Trigger>

Now whenever I click one of my buttons (there is 4), the marker object turns black. 现在,每当我单击一个按钮(有4个)时,标记对象都会变为黑色。 I move my mouse elsewhere and it's still black - so far so good. 我将鼠标移到其他地方,它仍然是黑色的-到目前为止,一切都很好。 Now, however, if I click another control on my application, the button loses focus (naturally) and therefore the marker isn't black anymore! 但是,现在,如果我单击应用程序上的另一个控件,该按钮将自然失去焦点,因此标记不再是黑色!

How can this be prevented? 如何预防? I want the marker to stay black when I click the button and once the button loses focus the marker should still stay black until I click the next button. 单击按钮时,我希望标记保持黑色,一旦按钮失去焦点,标记应仍保持黑色,直到单击下一个按钮。

It sounds more like you're looking for the kind of behaviour you'd get from ToggleButton , where the button is "checked" and "unchecked" by successive clicks. 听起来更像是您在寻找从ToggleButton中获得的行为,在该行为中,通过连续单击可以将按钮“选中”和“取消选中”。

Try swapping out your Button for a ToggleButton and adding a trigger for the IsChecked property rather than the properties you have now. 尝试将Button换成ToggleButton,并为IsChecked属性而不是现在的属性添加触发器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM