Question

I have to access to ScrollViewer in GridControl to do a synchronization with my other GridControls on scrolling.

Before I used vanilla DataGrid and it was quite easy to get ScrollViewer via VisualTreeHelper and then to subscribe to the ScrollChanged events.

I tried the same approach with GridControl but no luck. VisualTreeHelper.GetChildrenCount() gives me 0 and also LogicalTreeHelper.GetChildren() gives me an empty iterator.

But..

WPF Snooper can access logical and visual trees just perfectly perfect! So, what I've done wrong and why I am not able to get the information WPF Snooper is able to get?

Was it helpful?

Solution

That was the case! I tried to initialize it too early.

I did it in constructor, when it was initialized yet.

If someone needs solution how to synchronize data grids (it doesn't matter if this is vanilla DataGrid or DevExpress GridControl or mix of them) to show the same data when you are horizontally scrolling and you have more columns then you can show then here is the solution:

In your constructor:

    mainDataGrid.Loaded += (sender, args) =>
                           {
                               ScrollViewer sv = mainDataGrid.FindVisualTreeChild<ScrollViewer>();

                               if (sv != null)
                                   sv.ScrollChanged += DataGridScrollChanged;

                           };

Add the event handler for ScrollChanged event:

private void DataGridScrollChanged(object sender, ScrollChangedEventArgs e)
{
    if (e.HorizontalChange == 0.0f)
        return;
    ScrollViewer sv = dependentDataGrid1.FindVisualTreeChild<ScrollViewer>();
    if (sv != null)
        sv.ScrollToHorizontalOffset(e.HorizontalOffset);

    sv = dependentDataGrid2.FindVisualTreeChild<ScrollViewer>();
    if (sv != null)
        sv.ScrollToHorizontalOffset(e.HorizontalOffset);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top