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
- private void dgResults_MouseWheel(object sender, MouseWheelEventArgs e)
- {
- if (!e.Handled)
- {
- int rowsToMove = 0;
- if (e.Delta < 0)
- {
- rowsToMove = e.Delta / 120 * -1;
- }
- else
- {
- rowsToMove = e.Delta / 120 * -1;
- }
-
- if (dgResults.SelectedIndex == 0
- || dgResults.SelectedIndex == (dgResults.ItemsSource.Cast<ItemSourceType>().ToList().Count - 1))
- { return; }
-
- dgResults.SelectedIndex = dgResults.SelectedIndex + rowsToMove;
- dgResults.ScrollIntoView(dgResults.SelectedItem, dgResults.Columns[0]);
- }
- }
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!!