如何防止 ScrollViewer 使用 MouseWheel 事件
我正在构建 SL 应用程序,用于在布局上进行缩放和平移。一切工作正常,除了当我使用鼠标滚轮放大时,在一些缩放滚动条开始使用鼠标滚轮之后,我可以滚动而不是缩放。如果我将滚动条放在末尾或开头,我只能再次缩放。如何防止scrollviewer使用鼠标滚轮?我希望变焦只能通过滚轮操作。先感谢您!
这是我缩放内容时的 MouseWheel 方法的代码:
protected override void OnMouseWheel(MouseWheelEventArgs e)
{
base.OnMouseWheel(e);
if (e.Delta > 0)
{
this.aniScaleX.To += 0.2;
this.aniScaleY.To += 0.2;
this.sbScale.Begin();
}
else if (e.Delta < 0 && (this.aniScaleX.To > 1 && this.aniScaleY.To > 1))
{
this.aniScaleX.To -= 0.2;
this.aniScaleY.To -= 0.2;
this.sbScale.Begin();
}
Sizer.Width = Board.ActualWidth * (double)this.aniScaleX.To;
Sizer.Height = Board.ActualHeight * (double)this.aniScaleY.To;
I'm building SL application for zooming and panning across the layout. Everything is working fine, except that when I zoom in using mouse wheel , after some zoom scrollbars start to use mouse wheel so after that I can scroll not zoom. I only can zoom again if I put scrollbars at the end or begining. How to prevent scrollviewer from using mouse wheel? I want that zoom only be operated by wheel. Thank you in advance!
Here is my code of MouseWheel method when I'm zooming content :
protected override void OnMouseWheel(MouseWheelEventArgs e)
{
base.OnMouseWheel(e);
if (e.Delta > 0)
{
this.aniScaleX.To += 0.2;
this.aniScaleY.To += 0.2;
this.sbScale.Begin();
}
else if (e.Delta < 0 && (this.aniScaleX.To > 1 && this.aniScaleY.To > 1))
{
this.aniScaleX.To -= 0.2;
this.aniScaleY.To -= 0.2;
this.sbScale.Begin();
}
Sizer.Width = Board.ActualWidth * (double)this.aniScaleX.To;
Sizer.Height = Board.ActualHeight * (double)this.aniScaleY.To;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
尝试设置:
Try to set:
就我而言,ScrollViewer 之前总是收到事件,因为他位于可视化树的顶部。因此,我只是在鼠标滚轮事件的滚动查看器中注册了事件处理程序,并且总是在发生这种情况时,我只需将他重定向到执行缩放的“原始”鼠标滚轮功能。
我希望这能帮助那些像我一样被“困”在这里的人。谢谢大家的回答和建议..
In my case,ScrollViewer always received event before because he is on the top of the visual tree. So I just registered event handler in scrollviewer on mouse wheel event and always when It happens, I simply redirect him to my "original" mousewheel function which do zoom.
I hope so that this will help somebody who is "stuck" like me here. Thank you all on your answers and suggestions..
这花了我一段时间 (WPF),但基本上您应该在具有该事件的任何父 UIElement 中使用 AddHandler。
对我来说,我在主窗口中执行了此操作。所以我的代码看起来像:
这也意味着不重写 OnMouseWheelDown,而是创建一个与 RoutedEventHandler 委托匹配的方法,并将 e (这将是一个 RoutedEventArg)转换为 MouseWheelEventArgs 以访问确定缩放所需的属性。
我希望这对您的情况有所帮助。
This took me awhile (WPF), but basically you should use AddHandler in whatever parent UIElement that has the event.
For me, I did this in my MainWindow. So my code looked like:
This will also mean not overriding OnMouseWheelDown, but instead creating a method that matches the RoutedEventHandler delegate and casting e (which will be a RoutedEventArg) as MouseWheelEventArgs to get access to the properties required to determining zoom.
I hope this helps for your situation.