[UWP] Button控件不会在右键单击时触发视觉状态 [英] [UWP]The Button control doesn't trigger visual states on right click

查看:81
本文介绍了[UWP] Button控件不会在右键单击时触发视觉状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我注意到默认按钮控件中出现令人讨厌的行为,我似乎无法正确修复。



基本上,每当我右键单击一个按钮(我正在这样做,因为我想显示一个上下文菜单)时,按钮就会完全静止,并直接切换到默认的可视状态,完全没有动画。



在左键单击时,它会执行漂亮的显示指针向下动画,根网格向下和向上动画,并且显示光线从指针移开。



事实是,与 Button 不同, ListViewItemPresenter 完美地做到了这一点:右键单击它的行为与左键单击完全相同(在这里查看
GIF
)。在这个GIF中,我实际上只是右击,表明它处理得很好。



我的问题是:这是的错误按钮控制?为什么它的行为与此方案中的默认  ListViewItemPresenter 完全不同?
最重要的是有一种方法可以让它的行为就像  ListViewItemPresenter



注意:我已经尝试手动使用 VisualStateManager 来赚取
按钮 当按下指针(包括右键单击)时,手动转到" Pressed"  可视状态,但它几乎不会产生效果与 ListViewItemPresenter 中的那个一样好:
根网格向下/向上动画工作正常,但显示效果"毛刺"并且只在几分之一秒内变得可见,因为只要我释放右键单击打开上下文菜单,  按钮  立即
就会回落到默认的可视状态,导致揭示效果立即消失。



感谢您的帮助!

Hello, I've noticed an annoying behavior in the default Button control that I can't seem to be able to properly fix.

Basically, whenever I right-click a button (I'm doing that because I want to show a context menu), the button just stays perfectly still, and directly switches to the default visual state, with no animations at all.

On left click instead, it does the nice reveal pointer down animation, with both the root grid animating down and up, and the reveal light moving away from the pointer.

The thing is that, unlike the Button, the ListViewItemPresenter does this perfectly fine: right clicking it behaves exactly the same as left clicking it (see GIF here). In this GIF, I'm actually only right clicking, to show that it handles that just fine.

My questions are: is this a bug with the Button control? Why would it behave completely differently than the default ListViewItemPresenter in this scenario? And most importantly: is there a way to make it behave just like the ListViewItemPresenter?

Note: I've already tried to manually using the VisualStateManager to make the Button manually go to the "Pressed" visual state when the pointer is pressed (including the right click), but it doesn't result in an effect nearly as nice as the one in the ListViewItemPresenter: the root grid down/up animation works fine, but the reveal effect "glitches out" and becomes visible for just a fraction of a second, because as soon as I release the right click to open the context menu, the Button immediately falls back to the default visual state, causing the reveal effect to vanish instantly.

Thanks for your help!

推荐答案

这是预期的行为。如果你看一下Button和ListViewItem的样式,你会发现它们是用不同的控件实现的。 Button的模板是一个  ContentPresenter
而ListViewItem的模板是  ListViewItemPresenter 。 ListViewItemPresenter有一个名为  ​​ PressedBackground  
决定按下ListViewItem时的颜色。它显示了你的行为。但是,ContentPresenter不包含这样的属性,它需要使用  VisualStateManager来控制响应用户操作的不同行为,如
pointover和按。

This is behavior is expected. If you take a look at Button's and ListViewItem's style, you could found that they are implement with different controls. Button's template is a ContentPresenter while ListViewItem's template is a ListViewItemPresenter. ListViewItemPresenter has a property called PressedBackground that decides the color when the ListViewItem is pressed. And it shows the behavior that you got. But ContentPresenter doesn't contain such a property and it needs to use VisualStateManager to control different behavior that responses to user action like pointover and press.

>> "一旦我释放右键单击以打开上下文菜单,Button立即回落到默认的可视状态,导致显示效果立即消失。"

>>"as soon as I release the right click to open the context menu, the Button immediately falls back to the default visual state, causing the reveal effect to vanish instantly."

这是也期望作为VisualState:按下仅在按下按钮时有效。 

This is also expected as the VisualState:Press only works when the button is pressed. 

如果您想获得相同的行为,我建议您可能需要创建自定义按钮并使用其他演示者作为按钮的模板。

If you wants to get the same behavior, I suggest that you might need to create a custom button and use other presenter as the template of the button.

祝你好运,

Roy


这篇关于[UWP] Button控件不会在右键单击时触发视觉状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆