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?
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
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
Join a Chapter
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.