WPF/MVVM: Sync scrolling of two datagrids in different views

后端 未结 4 988
后悔当初
后悔当初 2021-02-09 02:03

I have two datagrids side by side bound to different data tables and each with their own view.

The datatables both have the same number of rows, and I want both grids to

4条回答
  •  滥情空心
    2021-02-09 02:37

    I was able to overcome this issue via some reflection hacks:

    
    
    

    and the code itself is:

        private void DataGrid1_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            if (e.HorizontalChange != 0.0f)
            {
                ScrollViewer sv = null;
                Type t = DataGrid1.GetType();
                try
                {
                    sv = t.InvokeMember("InternalScrollHost", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetProperty, null, DataGrid2, null) as ScrollViewer;
                    sv.ScrollToHorizontalOffset(e.HorizontalOffset);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    

提交回复
热议问题