Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
282 views
in Technique[技术] by (71.8m points)

c# - Get datagrid's scrollviewer

I'm trying to get the datagrid's scrollviewer to be able to set the offset (which has been stored earlier).

I use this function :

public static T GetVisualChild<T>(DependencyObject parent) where T : Visual       
{     
    T child = default(T);

    int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < numVisuals; i++)
    {
        Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
        child = v as T;
        if (child == null)
        {
            child = GetVisualChild<T>(v);
        }
        if (child != null)
        {
            break;
        }
    }
    return child;
}

And I call it like this :

this.dataGrid.ItemsSource = _myData;
ScrollViewer sc = ressource_design.GetVisualChild<ScrollViewer>(this.dataGrid);
if (sc != null) sc.ScrollToVerticalOffset(stateDatagrid.ScrollbarOffset);

And it works in many cases, but in some cases the function returns null and I'm not able to get the scrollviewer.

This call is made just after setting the ItemsSource (ObservableCollection of items) and it works well in 90% cases. The datagrid has not been rendered yet.

I've also tried with the function :

public static ScrollViewer GetScrollViewerFromDataGrid(DataGrid dataGrid)       
{        
    ScrollViewer retour = null;
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(dataGrid) && retour == null; i++)
    {
        if (VisualTreeHelper.GetChild(dataGrid, i) is ScrollViewer)
        {

            retour = (ScrollViewer)(VisualTreeHelper.GetChild(dataGrid, i));

        }
    }
    return retour;
}

still null.

Why I'm unable to get the datagrid's scrollviewer ?

I've not pasted my datagrid's style since I have datagrids working with it and it is complicated with many dependencies.

I thought it could be related to virtualization but i'm not able to retrieve the scrollviewer of this datagrid :

<DataGrid Style="{StaticResource StyleDataGrid}"  HeadersVisibility="None" ItemsSource="{Binding _Data}" Name="dataGrid1" RowDetailsVisibilityMode="Visible"  SelectionChanged="dataGrid1_SelectionChanged">
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to go recursive through the VisualTree elements. Your function only looks at DataGrid layer. If the ScrollViewer isn't there (see picture) you will not find it.

enter image description here

Try the following function:

public static ScrollViewer GetScrollViewer(UIElement element)
{
    if (element == null) return null;

    ScrollViewer retour = null;
    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element) && retour == null; i++) {
        if (VisualTreeHelper.GetChild(element, i) is ScrollViewer) {
            retour = (ScrollViewer) (VisualTreeHelper.GetChild(element, i));
        }
        else {
            retour = GetScrollViewer(VisualTreeHelper.GetChild(element, i) as UIElement);
        }
    }
    return retour;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...