I am making a wpf application that has 2 datagrids and I want them to scroll together. I know that the DataGridView class has a scroll event that you can use make the necessary changes to the other grid, but DataGrids don't have a Scroll event. I MUST use a DataGrid.
This example is good but is not WPF and it's using DataGridView instead of DataGrid. Using one scroll bar to control two DataGridView
What is the best way to have it so that one data grid's scroll bar also will move the data grid's scroll bar in WPF and DataGrids?
You can do this by getting the underlying ScrollViewer of both DataGrid's and setting up the event accordingly. Below is a quick example I made that appears to work as you've described.
xaml:
code behind:
What is happening is that I'm iterating through the VisualTree of both DataGrids upon Window load using getScrollbar. I then set up the scroll changed event for both DataGrids and then scroll to the vertical offset that was just changed inside the scroll changed event handler.