DataGrid scroll behavior

Nov 20, 2008 at 12:35 PM
Hi,

 I have a panel with lots of elements, so my panel requiers a scrollview.
 One of the elements is a datagrid. 

 The problem is that when I scroll using the mouse weel the the scrolling gets stuck when the pointer is over the datagrid.
 The expected behavior should be the grid should scroll to the last element and then the panel should continue scrolling.

 Any idea on how this can be accomplished?

Regards,
Calin,


Coordinator
Nov 21, 2008 at 2:26 AM
Edited Nov 21, 2008 at 2:27 AM
Hi Calin,

The behavior you are seeing is a not a problem with DataGrid as is but a much general behavior when you have nested ScrollViewers. In such a case the MouseWheel event is always handled by the inner scroll viewer. In this case DataGrid itself has a ScrollViewer and the event is handled by it even though it looks like it is not doing any thing (because it didnt have to do anything).

You can try the same with ListBox too. Put a ListBox inside a ScrollViewer and see what happens when you mouse wheel pointing on the list box. The result should be pretty much same because the ListBox too has a scroll viewer within itself.

Now speaking of the solution, you can make the outer scroll viewer trap the preview event and handle it like....

    public class MyScrollViewer : ScrollViewer
    {
        // Literraly this code should be sufficient
        protected override void OnPreviewMouseWheel(MouseWheelEventArgs e)
        {
            base.OnPreviewMouseWheel(e);
            OnMouseWheel(e);
        }
    }

and use this as your scroll viewer instead. Also you could handle Mouse.PreviewMouseWheel event on the outer scroll viewer and simulate the scroll behavior your self.

Note that either of these approaches mean that the DataGrid or the ListBox within the ScrollViewer will not get the wheel notifications (which is by default).
Nov 26, 2008 at 7:39 AM
Hi VamseeP,

 Thank you for your reply, what I was actually looking for is something like this http://serialseb.blogspot.com/2007/09/wpf-tips-6-preventing-scrollviewer-from.html

Hope this helps someone else.

Regards,