문제

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?

도움이 되었습니까?

해결책

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);
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top