如何在Silverlight中查找可见的DataGrid行?

最佳答案

我不确定Visible DataGridRow是什么意思,但是您可以通过在可视树中找到它们来获得当前生成的所有DataGridRow。基本上,这将为您提供所有可见的DataGridRow,由于DataGrid中使用的虚拟化功能,可能还会提供更多的可见性



private List<DataGridRow> GetDataGridRows(DataGrid dataGrid)
{
    return GetVisualChildCollection<DataGridRow>(c_dataGrid);
}


GetVisualChildCollection

public static List<T> GetVisualChildCollection<T>(object parent) where T : FrameworkElement
{
    List<T> visualCollection = new List<T>();
    GetVisualChildCollection(parent as DependencyObject, visualCollection);
    return visualCollection;
}
private static void GetVisualChildCollection<T>(DependencyObject parent, List<T> visualCollection) where T : FrameworkElement
{
    int count = VisualTreeHelper.GetChildrenCount(parent);
    for (int i = 0; i < count; i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(parent, i);
        if (child is T)
        {
            visualCollection.Add(child as T);
        }
        else if (child != null)
        {
            GetVisualChildCollection(child, visualCollection);
        }
    }
}

10-08 20:28