SIGN UP MEMBER LOGIN:    
ARTICLE

Mouse Wheel Scroll For ScrollViewer in Silverlight 3

Posted by Diptimaya Patra Articles | Silverlight with C# December 16, 2009
In this article I would give you a Trick to scroll your ScrollViewer with Mouse Wheel.
Reader Level:

Introduction

In this article I would give you a Trick to scroll your ScrollViewer with Mouse Wheel.

Creating Silverlight Project

Fire up Visual Studio 2008 and create a new Silverlight 3 Project. Name it as MouseScrollInScrollViewerNavApp.

ScrollViewer1.gif

Open the Solution in Expression Blend 3 and do add ScrollViewer with some other controls.

I have just created a long height page where the ScrollViewer Scrollbar is visible.

Have the DLL Reference of
System.Windows.Controls.Toolkit

Keep the Frame inside the ScrollViewer.

<ScrollViewer x:Name="SVFrame">
                                <navigation:Frame x:Name="ContentFrame" Style="{StaticResource ContentFrameStyle}"
                                                Source="/Home" Navigated="ContentFrame_Navigated" NavigationFailed="ContentFrame_NavigationFailed">
                                                <navigation:Frame.UriMapper>
                                                                <uriMapper:UriMapper>
                                                                                <uriMapper:UriMapping Uri="" MappedUri="/Views/Home.xaml"/>
                                                                                <uriMapper:UriMapping Uri="/{pageName}" MappedUri="/Views/{pageName}.xaml"/>
                                                                </uriMapper:UriMapper>
                                                </navigation:Frame.UriMapper>
                                </navigation:Frame>
                </ScrollViewer>


ScrollViewer2.gif

Now if you see above image it is obvious that the Scrollbar will be visible when the height of the page is exceeded the height of the Frame.

Now, if you want to Scroll with Mouse Wheel, you need to use the following method in the MainPage.xaml.cs' Constructor.

public MainPage()
        {
            InitializeComponent();
            SVFrame.SetIsMouseWheelScrollingEnabled(true);
        }

Now you are able to Scroll with Mouse Wheel.

The extension methods come with the Silverlight 3 Toolkit DLL.

ScrollViewer3.gif

Now one more Trick to share with.

When you browse from one Page to another and come back the Page is having the last Scrolled persisted.

Look at the following image and you would understand.

ScrollViewer4.gif

You can fix the issue by adding the following code in ContentFrame_Navigated method in MainPage.xaml.cs.

private void ContentFrame_Navigated(object sender, NavigationEventArgs e)
        {
            SVFrame.ScrollToTop();
        }

Now whenever you navigate to any page the ScrollViewer will Scroll to Top for you.

Hope you guys like the Tricks.

Login to add your contents and source code to this article
Article Extensions
Contents added by Tom Hamilton on Feb 09, 2010
Unable to access source link... ? broken?
share this article :
post comment
 

Hi Diptimaya Patra, i'm trying to adapt this code to WPF can you help me, but i didn't locate the property SetIsMouseWheelScrollingEnabled, in the WPF Scroll Viewer. Thanks in advance. Daniel Benevides

Posted by Daniel Benevides Jan 12, 2012

Diptimaya Patra, great thanks to you!!!

Posted by Lina Fetisova May 12, 2010

Diptimaya Patra, thank you a lot for your article
may I ack you to send me a sample too to such an email: ange-linka@list.ru

thank you a lot!

Posted by Lina Fetisova May 11, 2010

Send me your mail I would send you the sample.

Thanks
Diptimaya Patra

Posted by Diptimaya Patra Mar 02, 2010

I am trying to utilize the method in this post but for some reason it just won't work.

I have the dll reference in the app library. I am using Silverlight 3 just like the example but can't get it to function.

Also, the link to the zip file is broken.

Would really appreciate some help with this method.

THNX

Posted by Caren Khachatrian Mar 01, 2010
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Team Foundation Server Hosting
Become a Sponsor