简体   繁体   English

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

[英]How to find visible DataGrid rows in Silverlight?

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

I'm not sure what you mean by Visible DataGridRow s but you could get all DataGridRow s that are generated at the moment by finding them in the Visual Tree. 我不确定Visible DataGridRow的含义,但是可以通过在可视树中找到它们来获取当前生成的所有DataGridRow This will basically give you all Visible DataGridRow s and probably a few more because of the Virtualization used in the DataGrid 基本上,这将为您提供所有Visible DataGridRow并且可能还会为您提供更多信息,因为DataGrid使用了虚拟化

Example

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

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

The way I've done it is by hooking up to the DataGrid's LoadingRow and UnloadingRow events. 我这样做的方法是连接到DataGrid的LoadingRowUnloadingRow事件。

Here's an example 这是一个例子

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM