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

时间:2011-02-22 14:04:56

标签: c# silverlight

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

2 个答案:

答案 0 :(得分:1)

我不确定Visible DataGridRow的含义,但是您可以通过在Visual Tree中找到它们来获取当前生成的所有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);
        }
    }
}

答案 1 :(得分:0)

我这样做的方法是联系DataGrid的LoadingRowUnloadingRow事件。

这是一个例子

    HashSet<DataGridRow> loadedRows

    private void HandleUnloadingRow(object sender, DataGridRowEventArgs e)
    {
        _loadedRows.Remove(e.Row);
    }

    private void HandleLoadingRow(object sender, DataGridRowEventArgs e)
    {
        _loadedRows.Add(e.Row);
    }