惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
V
Visual Studio Blog
P
Privacy International News Feed
月光博客
月光博客
Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
Lohrmann on Cybersecurity
N
News and Events Feed by Topic
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Apple Machine Learning Research
Apple Machine Learning Research
阮一峰的网络日志
阮一峰的网络日志
Webroot Blog
Webroot Blog
T
Threatpost
宝玉的分享
宝玉的分享
The Last Watchdog
The Last Watchdog
小众软件
小众软件
L
LINUX DO - 最新话题
C
Cisco Blogs
T
Troy Hunt's Blog
Schneier on Security
Schneier on Security
酷 壳 – CoolShell
酷 壳 – CoolShell
www.infosecurity-magazine.com
www.infosecurity-magazine.com
雷峰网
雷峰网
G
GRAHAM CLULEY
有赞技术团队
有赞技术团队
Know Your Adversary
Know Your Adversary
博客园 - 叶小钗
罗磊的独立博客
V
V2EX
博客园 - Franky
P
Proofpoint News Feed
SecWiki News
SecWiki News
腾讯CDC
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Jina AI
Jina AI
博客园 - 三生石上(FineUI控件)
S
Secure Thoughts
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Google DeepMind News
Google DeepMind News
Attack and Defense Labs
Attack and Defense Labs
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
PCI Perspectives
PCI Perspectives
V2EX - 技术
V2EX - 技术
Google DeepMind News
Google DeepMind News
Last Week in AI
Last Week in AI
aimingoo的专栏
aimingoo的专栏
Cisco Talos Blog
Cisco Talos Blog
N
News and Events Feed by Topic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
SegmentFault 最新的问题

博客园 - Adrian H.

Hello in 2025 Hello World LINQ表达式中关于显式范围变量的Bug jQuery太好使了 一次性下载.NET源码和pdb,并在VS2005中进行调试 Windows Server 2008: Server Core初体验 - Adrian H. C# Future Focus: 动态查找(Dynamic Lookup) Volta : Democratizing the Cloud, 1st CTP Released! Parallel Extensions for .NET 3.5 CTP! Silverlight 2.0, .NET类库源码, ASP.NET MVC... VS 2008与老版本插件的兼容性问题导致Crash Silverlight Tools for VS 2008 RTM [C# 3.0] 传递匿名类型对象的问题 Visual Studio 2008 RTM! 在 .NET Framework x (x < 3.5) 环境下运行带有扩展方法(Extension Method)的程序 使用ActionScript 3中的反射,实现单元测试TestRunner ASP.NET MVC Framework Announced .NET Framework 源代码将于今年发布 N-Tiers?
为Silverlight控件添加鼠标滚轮支持 - Adrian H. - 博客园
Adrian H. · 2009-02-13 · via 博客园 - Adrian H.

其实网上有挺多实现鼠标滚轮支持的文章,原理都一样,通过 HtmlPage.Window.AttachEvent("DOMMouseScroll", ...)来对ScrollViewer进行控制,但许多却仅限于对ScrollViewer添加滚轮支持,而对TextBox、ListBox这样的控件 好像挺没辙的。

使用Reflector看TextBox和ListBox的实现,发现他们内部都有一个field为ScrollViewer,只要读到这个 field,应该就能对它们的滚动进行控制。我尝试使用反射获取它们的ScrollViewer,但Silverlight的安全机制和完全版的CLR不 同,它不允许我获得非public的成员。。。

突然...从Reflector看到TextBox和ListBox都使用了父类的GetTemplateChild方法获取 ScrollViewer对象,而GetTemplateChild是protected方法,所以只需要创建一个类,继承TextBox或者 ListBox,再使用GetTemplateChild来暴露他们的ScrollViewer!

namespace Xin.Silverlight.MouseWheelSupport
{
    
public class ListBox : System.Windows.Controls.ListBox, IScrollable
    {
        ScrollViewer sw;
public ScrollViewer ScrollViewer
        {
            
get
            {
                
if (sw == null)
                {
                    sw 
= GetTemplateChild("ScrollViewer"as ScrollViewer;
                }
                
return sw;
            }
        }
    }
}

 代码中的IScrollable是自己写的一个小接口,声明实现它的类必须拥有一个ScrollViewer property。示例代码中只对TextBox和ListBox实现了该接口,假设需要对GridView等控件添加滚轮支持,只需实现这个接口并暴露 出它的ScrollViewer对象。

 稍稍整理了一下代码,只需要执行MouseWheelSupportAddOn.Activate(this.LayoutRoot, true)便可以对整个Page的可支持滚轮的控件添加滚轮支持。

Activate方法可以接受ContentControl,Panel,IScrollable对象,第二个参数指定是否需要对控件的Children添加滚轮支持。

【代码和示例程序下载】