Sharing 1 scrollbar between two DataGrids

I am creating a wpf application that has 2 datagrids in it and I want them to scroll together. I know that the DataGridView class has a scroll event that you can use to make the necessary changes to another grid, but DataGrids do not have a Scroll event. I MUST use a DataGrid.

This example is good, but not WPF, and it uses a DataGridView instead of a DataGrid. Using one scrollbar to control two DataGridViews

What is the best way to get it so that one data grid scroll bar also moves the data grid scroll bar in WPF and DataGrids?

+5
source share
1 answer

, ScrollViewer DataGrid, . , , , .

XAML:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded">
    <Grid>
        <DataGrid AutoGenerateColumns="True" Height="200" HorizontalAlignment="Left" Margin="52,69,0,0" Name="dataGrid1" VerticalAlignment="Top" Width="200" ItemsSource="{Binding Collection}" />
        <DataGrid AutoGenerateColumns="True" Height="200" HorizontalAlignment="Left" Margin="270,69,0,0" Name="dataGrid2" VerticalAlignment="Top" Width="200" ItemsSource="{Binding Collection}" />
    </Grid>
</Window>

:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;

namespace WpfApplication1
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        ObservableCollection<Person> _collection = new ObservableCollection<Person>();
        ScrollViewer scrollView = null;
        ScrollViewer scrollView2 = null;

        public MainWindow()
        {
            for (int i = 0; i < 50; i++)
            {
                var p = new Person() { Name = string.Format("{0}", i), Age = i };
                _collection.Add(p);
            }
            this.DataContext = this;
            InitializeComponent();
        }

        void scrollView_ScrollChanged(object sender, ScrollChangedEventArgs e)
        {
            var newOffset = e.VerticalOffset;

            if ((null != scrollView) && (null != scrollView2))
            {
                scrollView.ScrollToVerticalOffset(newOffset);
                scrollView2.ScrollToVerticalOffset(newOffset);
            }
        }

        public ObservableCollection<Person> Collection
        {
            get
            {
                return _collection;
            }
        }

        private ScrollViewer getScrollbar(DependencyObject dep)
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dep); i++)
            {
                var child = VisualTreeHelper.GetChild(dep, i);
                if ((null != child) && child is ScrollViewer)
                {
                    return (ScrollViewer)child;
                }
                else
                {
                    ScrollViewer sub = getScrollbar(child);
                    if (sub != null)
                    {
                        return sub;
                    }
                }
            }
            return null;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            scrollView = getScrollbar(dataGrid1);
            scrollView2 = getScrollbar(dataGrid2);

            if (null != scrollView)
            {
                scrollView.ScrollChanged += new ScrollChangedEventHandler(scrollView_ScrollChanged);
            }
            if (null != scrollView2)
            {
                scrollView2.ScrollChanged += new ScrollChangedEventHandler(scrollView_ScrollChanged);
            }
        }
    }

    public class Person
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }
}

, , VisualTree DataGrids on Window getScrollbar. DataGrids, , .

+7

All Articles