获取datagrid的scrollviewer [英] Get datagrid's scrollviewer

查看:236
本文介绍了获取datagrid的scrollviewer的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图让datagrid的scrollviewer能够设置偏移量(该偏移量已存储在前面)。

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

我使用此功能:

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;
}

我这样称呼它:

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

它在很多情况下都可以工作,但是在某些情况下,该函数返回null并且我无法获取scrollviewer。

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

此调用是在设置ItemsSource(项目的ObservableCollection)之后进行的,并且在90%的情况下效果很好。

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;
}

仍然为空。

为什么我无法获得datagrid的scrollviewer?

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

我没有粘贴datagrid的样式,因为我有使用datagrid的样式,并且它存在很多依赖关系。

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

我以为它可能与虚拟化有关,但是我无法检索此datagrid的scrollviewer:

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">


推荐答案

您需要通过<$ c $进行递归c> VisualTree 元素。您的函数仅查看 DataGrid 层。如果没有 ScrollViewer (请参见图片),您将找不到它。

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.

尝试以下功能:

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;
}

这篇关于获取datagrid的scrollviewer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆