Get datagrid's scrollviewer

后端 未结 1 631
天命终不由人
天命终不由人 2020-12-11 19:57

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 G         


        
相关标签:
1条回答
  • 2020-12-11 20:19

    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.

    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;
    }
    
    0 讨论(0)
提交回复
热议问题