Problem:
If my DataGrid
is not entirely visible (horizontal & vertical scrollbars are showing) and I click on one of my cells that is p
As Dr.WPF has answered a similar question here the RequestBringIntoView should be handled in ItemsPanel.
Define an EventSetter
in the DataGrid.RowStyle
to call a handler that prevents the row from being brought into view:
XAML
<DataGrid>
<DataGrid.RowStyle>
<Style TargetType="{x:Type DataGridRow}">
<EventSetter Event="Control.RequestBringIntoView" Handler="DataGrid_Documents_RequestBringIntoView" />
</Style>
</DataGrid.RowStyle>
</DataGrid>
Handler
private void DataGrid_Documents_RequestBringIntoView(object sender, RequestBringIntoViewEventArgs e)
{
e.Handled = true;
}
I'm not sure this is working but here is an idea based on some investigation is made in the DataGrid's source code using Reflector:
1/ create a class which inherits DataGridCellsPanel. This is the Panel that is used internally by the DataGrid in order to arrange the cells
2/ override the BringIndexIntoView method by an empty method (without calling the base method)
3/ set the ItemsPanelTemplate property in your XAML:
<tk:DataGrid>
<tk:DataGrid.ItemsPanel>
<ItemsPanelTemplate>
<local:DataGridCellsPanelNoAutoScroll />
</ItemsPanelTemplate>
</tk:DataGrid.ItemsPanel>
</tk:DataGrid>
It seems that when a MouseDown event occurs, at some point the BringIndexIntoView method of the panel is called to do the auto-scroll. Replacing it with a no-op might do the trick.
I hadn't time to test this solution, please let us know if it's working.