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 forRequestBringIntoViewon allUserControlsthat make up my cells, and tried handling the event, thinking that maybe I wasn’t doing enough by just handlingRequestBringIntoViewon theDataGriditself. This did not work. - Hosted the
DataGridinside of aScrollViewer, and handled theScrollViewer‘sRequestBringIntoViewevent. This actually works, and stops the auto-scrolling behavior, but in my case hosting aDataGridinside of aScrollVieweris 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?
You can access the DataGrid’s internal ScrollViewer by modifying the template. Although normally you wouldn’t put an event handler to code behind in a template, if you declare the template inline you can treat the event handler the same way you are when you attach it to the DataGrid itself. This is the default template as generated from Blend including an added handler on the ScrollViewer for the RequestBringIntoView event: