WPF DataGrid: how do I stop auto scrolling when a cell is clicked?

前端 未结 9 1154
旧巷少年郎
旧巷少年郎 2020-12-08 07:31

Problem:
If my DataGrid is not entirely visible (horizontal & vertical scrollbars are showing) and I click on one of my cells that is p

9条回答
  •  北荒
    北荒 (楼主)
    2020-12-08 07:52

    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:

    var scp = TreeHelper.FindVisualChild(this.datagrid);
    scp.RequestBringIntoView += (s, e) => e.Handled = true;
    

    We use the ScrollContentPresenter because it's just below the ScrollViewer in the visual tree.

    Hope this helps !

提交回复
热议问题