WPF - 按住鼠标左键打开上下文菜单
在 Google Chrome 中,我真的很喜欢用鼠标左键按住“后退”按钮来获取完整浏览历史记录的功能。
在我的 WPF 应用程序中:对于带有上下文菜单的按钮,如何在按住鼠标左键(当然仍然使用常规右键单击)的情况下打开菜单?
In Google Chrome I really like the functionality of the left mouse hold down on the Back button to get the full browse history.
In my WPF app: for a button with a context menu, how can I open the menu on hold down of the left mouse (and of course still with the regular right click)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我建议通过启动计时器来处理
MouseDown
事件。如果触发MouseUp
事件,则需要停止计时器。您可以使用DispatcherTimer
来实现此目的。然后,您可以设置一个时间,在该时间后触发Timer_Tick
事件,您可以在其中执行您想要执行的操作。为了避免冒泡MouseDown
和MouseUp
事件出现问题,我建议在窗口构造函数中添加这两个处理程序,而不是在 XAML 中添加它们(至少将事件在我的示例代码中没有触发,所以我改变了)通过使用此外,您需要在那里设置计时器:
向窗口类添加一个字段:
并使用您想要等待的时间设置计时器<代码>Timer_Tick 事件被触发(也在窗口构造函数中):
然后您只需要处理事件即可完成:
希望有所帮助。
I would suggest to handle the
MouseDown
event by starting a timer there. If theMouseUp
event is fired, the timer needs to be stopped. You can use aDispatcherTimer
for that. Then you can set up a time after that theTimer_Tick
event should be fired, where you can perform the action you would like to perform. In order to avoid problems with the bubblingMouseDown
andMouseUp
events, I would suggest to add the two handlers in the window constructor instead of adding them in XAML (at least the events did not fire in my example code, so i changed that) by usingAdditionally, you need to set up the timer there:
Add a field to the window class:
and set up the timer with the time you want to wait until the
Timer_Tick
event is fired (also in the window constructor):Then you only need to handle the events and you are done:
Hope that helps.
我认为你唯一的方法是手动处理按钮上的 MouseDown/Move/Up 事件,在 MouseDown 发生后等待一段时间,如果在这段时间内你没有 MouseMove 或 MouseUp 事件,那么手动显示上下文菜单。如果显示上下文菜单,则必须注意按钮不要在此之后生成 Click 事件并执行默认的单击操作。
I think your only way is to manually process MouseDown/Move/Up events over the button, wait for a certain amount of time to pass after MouseDown occured and if in this amount of time you don't have a MouseMove or MouseUp event, then manually show the ContextMenu. If you show the context menu, you'll have to take care for the button not to generate a Click event after that and to do it's default click action.