Geeks With Blogs

News
Steve Clements .Net and then some....

By Default scrolling with the mouse wheel is not enabled in the Silverlight DataGrid, actually I don’t think any control has it enabled.

Personally I think its pretty standard functionality for things to scroll with the mouse wheel.

So, I have got this little chunk of code that takes does the trick, nothing fancy going on here, but it works a treat.

Code Snippet
  1. private void dgResults_MouseWheel(object sender, MouseWheelEventArgs e)
  2. {
  3.     if (!e.Handled)
  4.     {
  5.         int rowsToMove = 0;
  6.         if (e.Delta < 0)
  7.         {
  8.             rowsToMove = e.Delta / 120 * -1;
  9.         }
  10.         else
  11.         {
  12.             rowsToMove = e.Delta / 120 * -1;
  13.         }
  14.  
  15.         if (dgResults.SelectedIndex == 0
  16.                 || dgResults.SelectedIndex == (dgResults.ItemsSource.Cast<ItemSourceType>().ToList().Count - 1))
  17.         { return; }
  18.  
  19.         dgResults.SelectedIndex = dgResults.SelectedIndex + rowsToMove;
  20.         dgResults.ScrollIntoView(dgResults.SelectedItem, dgResults.Columns[0]);
  21.     }
  22. }

 

 

To explain a little…I found that my delta would change in multiples of 120, depending on how fast I scrolled, –120 if I scrolled down, +120 is I scrolled up.  I needed to change this as if I scrolled down I needed the SelectedIndex to increase, so that’s what the first bit does.

Then I check that I’m not at either 0 or the last item in the datagrid, set the selected index and use the ScrollToView() method.

You may need to change slightly if you have horizontal scrolling, to stay with the correct column, for me having the first column is more than ideal, perfect in fact.

Note: This code has been tested nowhere except on  my dev machine. So, here goes!!

Posted on Friday, November 13, 2009 10:27 AM Silverlight | Back to top


Comments on this post: Silverlight DataGrid: Scroll with mouse wheel…quick and dirty!

# re: Silverlight DataGrid: Scroll with mouse wheel…quick and dirty!
Requesting Gravatar...
nice dude!

there are so many loooooooooooong examples for doing this what you've done in some lines! well done
Left by J.akhtar on Jan 11, 2010 11:27 PM

# re: Silverlight DataGrid: Scroll with mouse wheel…quick and dirty!
Requesting Gravatar...
Hi thanks for this. Just a question what is the fully qualified path for ItemSourceType? Thanks.
Left by dave on Feb 11, 2010 10:59 AM

# re: Silverlight DataGrid: Scroll with mouse wheel…quick and dirty!
Requesting Gravatar...
awesome solution man... real quick and works a charm ... who really wants the four pointed mouse scroll icon to work automatically.... till the time normal scroll works...

great work...
Left by aarkay on May 26, 2011 8:11 PM

# re: Silverlight DataGrid: Scroll with mouse wheel…quick and dirty!
Requesting Gravatar...
works great, thanks.. dont know why this was not a built in feature, seems pretty standard.
Left by AC on Sep 07, 2011 3:48 PM

Your comment:
 (will show your gravatar)


Copyright © Steve Clements | Powered by: GeeksWithBlogs.net