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

前端 未结 9 1122
旧巷少年郎
旧巷少年郎 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:51

    I had the same problem as Rumit, but found a solution/hack.

    I thought if I could find a way to differentiate between mouse clicks and arrow keys, then I could set e.Handled accordingly.

    After some experimentation I found that e.OriginalSource changed depending on mouse or arrow key. For a mouse click, the handler for RequestBringIntoView is called once and e.OriginalSource was of type DataGridCell. For an arrow key, the handler is called twice and e.OriginalSource is of types DataGridRow and then DataGridCell.

    The code for my handler is:

    e.Handled = (e.OriginalSource is DataGridCell);
    

    This seems like a bit of a hack, but works great for me.

提交回复
热议问题