在C# 中,两个ScrollViewer嵌套在一起或者ScrollViewer里面嵌套一个ListBox、Listview(控件本身有scrollviewer)的时候,我们本想要的效果是鼠标滚动整个ScrollViewer的内容,但你会发现实际结果和你想像的不一样,内部有scrollviewer的内容不会随鼠标的滚动而滚动,怎么办呢?

解决办法如下:
先截取内部的ScrollViewer的滚动事件,然后再重新让外部的ScrollViewer触发滚动事件就可以了

假设有2个控件,一个是Listbox(内部名字是listbox),一个是ScrollViewer(外部),Listbox在ScrollViewer内部的,并且ListBox的scroll滚动功能都禁用为Disabled,只启用外部ScrollViewer的滚动功能,这时添加如下代码,则能使ListBox中的内容随ScrollViewer的鼠标滚动而滚动

listbox.PreviewMouseWheel+=(sender,e)=>
{
var eventArg = new MouseWheelEventArgs(e.MouseDevice,e.TimeDevice,e.Delta);
eventArg.RoutedEvent = UIElement.MouseWheelEvent;
eventArg.Source = sender;
this.listbox.RaiseEvent(eventArg);
};

如果 是在模版内部,拿不到listbox实例 ,可用sender强转成对象示例。有多级嵌套同理,都把事件路由上去

凡是含有ScrollViewer的控件或者ScrollViewer自身都适合

WPF滚动条嵌套,响应鼠标滑轮事件的处理的更多相关文章

  1. 滚动条响应鼠标滑轮事件实现上下滚动的js代码

    <script type="text/javascript"> var scrollFunc=function(e){ e=e || window.event; if( ...

  2. OpenCV鼠标滑轮事件

    鼠标的滑轮事件实现图像的缩放很方便,具体在回调函数中如下写: 其中scale可以在外部定义为全局变量,通过响应CV_EVENT_MOUSEWHEEL滑轮事件获取Scale的具体值. 获取Scale值需 ...

  3. 鼠标滑轮事件QWheelEvent

    一般鼠标滑轮事件会发出信号,参数是QWheelEvent,只需要新建槽函数,QWheelEvent作为参数. void myMouseWheelEvent(QWheelEvent* even) {)/ ...

  4. opencv —— setMouseCallback 响应鼠标操作事件

    鼠标操作:setMouseCallback 函数 借助回调函数,实现对鼠标每次操作的相应,即每进行一步鼠标操作,都会执行一次回调函数. void setMouseCallback(const stri ...

  5. JS鼠标滑轮事件的写法和按键的事件

    在body注册一下滑轮事件 <body onload="win_onload();"></body> 然后JS代码如下: function win_onlo ...

  6. javascript滚动栏响应鼠标滑轮的实现上下滚动事件

    实现鼠标滚动滚轮事件: <script type="text/javascript"><pre name="code" class=" ...

  7. Qt中隐藏滚动条重新实现鼠标滚轮事件wheelEvent

    delta()已经被弃用了,QT5中用的是angleDelta(),计算的时候取angleDelta().y()值. #重载方法wheelEvent(self,event),即滚轮事件方法 #---- ...

  8. Winform鼠标滑轮控制自定义滚动条

    场景:类似QQ聊天的窗体中,需要添加自定义滚动条vScroll.主窗体中panel存放空间,右边有垂直的滚动条vScroll. 问题:已经实现vScroll和Panel.VerticalScroll滚 ...

  9. WPF通过鼠标滑轮缩放显示图片

    如果你使用WinForm比较难实现通过滚动鼠标滑轮来对图片进行缩放显示,那么,你应该考虑一下使用WPF,既然是下一代Windows客户端开发平台,明显是有一定优势的,不然,MS是吃饱了撑着.   首先 ...

随机推荐

  1. 关于CoordinatorLayout与Behavior的一点分析

    Behavior是Android新出的Design库里新增的布局概念.Behavior只有是CoordinatorLayout的直接子View才有意义.可以为任何View添加一个Behavior.Be ...

  2. [Angular Directive] Structure directive and <template>

    The structure directive is just a sugar syntax of <template>. Such as: <div *ngIf="nam ...

  3. 1069. The Black Hole of Numbers (20)【模拟】——PAT (Advanced Level) Practise

    题目信息 1069. The Black Hole of Numbers (20) 时间限制100 ms 内存限制65536 kB 代码长度限制16000 B For any 4-digit inte ...

  4. [React] Recompose: Override Styles & Elements Types in React

    When we move from CSS to defining styles inside components we lose the ability to override styles wi ...

  5. java完美equals方法代码段

    public boolean equals(Object otherObject) { if(this == otherObject) { // 检測this与otherObject是否引用同一个对象 ...

  6. python3 numpy基本用法归纳总结

    安装numpy : pip install numpy numpy数组生成方法总结 In [4]: import numpy as np #使用列表生成一个一维数组 data = [1,2,3,4,5 ...

  7. scala读写文件 comparing values of types Unit and Int using `!=' will always yield true

    由于scala没有对写入文件的支持,所以写文件时通常借助java进行IO操作 //方式一(小文件) /* val s1 = Source.fromFile("D:\\inputword\\h ...

  8. 存储用es,消息队列用redis

    自动化确实方便,做微服务再合适不过了,单一jar包部署和管理都非常方便.只要系统架构设计合理,大型项目也能用.最近做的项目,统计中心和推荐系统,collector.calculator.recomme ...

  9. 修改MessageBox的标题的做法

    作者:朱金灿 来源:http://blog.csdn.net/clever101 1.用Win API的::MessageBox或CWnd::MessageBox代替AfxMessageBox. 2. ...

  10. Shell Step by Step (3) —— Stdin &amp; if

    4.输入输出 #! /bin/bash # Read users input and then get his name read -p "Please input your first n ...