Problem:
If my DataGrid
is not entirely visible (horizontal & vertical scrollbars are showing) and I click on one of my cells that is partially visible, the grid auto-scrolls to bring that cell into view. I don't want this to happen. I've tried playing around with RequestBringIntoView
, like this:
private void DataGrid_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
{
e.Handled = true;
}
But that does nothing.
Things I've tried:
- My cells are custom
UserControls
; I tried putting an event handler forRequestBringIntoView
on allUserControls
that make up my cells, and tried handling the event, thinking that maybe I wasn't doing enough by just handlingRequestBringIntoView
on theDataGrid
itself. This did not work. - Hosted the
DataGrid
inside of aScrollViewer
, and handled theScrollViewer
'sRequestBringIntoView
event. This actually works, and stops the auto-scrolling behavior, but in my case hosting aDataGrid
inside of aScrollViewer
is not at all desirable, so I need to come up with a different solution.
I'm not sure how to stop this behavior, any ideas?
I took more time to have a look at this problem as my first solution wasn't working.
However the answer of John is almost the good one. The trick is to catch the RequestBringIntoView event BEFORE it gets to the ScrollViewer in order to mark it has handled.
If you don't have to refine the whole template, you can use the following code:
We use the ScrollContentPresenter because it's just below the ScrollViewer in the visual tree.
Hope this helps !
As Dr.WPF has answered a similar question here the RequestBringIntoView should be handled in ItemsPanel.
Here's what worked for me (after trying all of the less complex "answers" to date):
ScrollViewer.CanContentScroll="False" seems mindbogglingly counter intuitive...