WPF中如何处理鼠标滚轮点击事件?
我想在单击鼠标滚轮时关闭选项卡控件中的选项卡。 如何在 WPF 中捕获此事件?
编辑: 这是代码:
private void tabMain_MouseDown(object sender, MouseButtonEventArgs e)
{
if(e.ChangedButton == MouseButton.Middle && e.ButtonState == MouseButtonState.Pressed)
{
MessageBox.Show("Middle button clicked");
}
}
I want to close a tab in my tab control when the mouse wheel is clicked. How can I capture this event in WPF?
EDIT:
Here's the code:
private void tabMain_MouseDown(object sender, MouseButtonEventArgs e)
{
if(e.ChangedButton == MouseButton.Middle && e.ButtonState == MouseButtonState.Pressed)
{
MessageBox.Show("Middle button clicked");
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
鼠标滚轮实际上是中间按钮,因此在 MouseDown 事件上滚轮单击的条件是 ChangedButton == Middle && ButtonState == 按下
Mousewheel is actually the MiddleButton, So the condition for Wheel click on a MouseDown event is ChangedButton == Middle && ButtonState == Pressed
更简单的解决方案
An even easier solution
如果有人尝试捕获此事件但失败,请检查发送者是否有鼠标手势“拦截”并在到达 MouseDown 事件之前对其进行处理。
这是我使用 HelixTookit 库中的 HelixViewport3D 的情况。 仅在我取消以下手势后,MouseWheelDown 才会在 MouseDown 事件中捕获:
希望它能为您节省一些时间。
In case someone is trying to catch this event but fails, check if sender has mouse gestures "intercepting" and handling it before it reaches MouseDown event.
Here is my situation with HelixViewport3D from HelixTookit library. MouseWheelDown is caught in MouseDown event only after i nullify following gesture:
Hope it'll save you some time.