开发者

How to find visible DataGrid rows in Silverlight?

How to find visible DataGrid rows in Silverlight?开发者_JAVA百科


I'm not sure what you mean by Visible DataGridRows but you could get all DataGridRows that are generated at the moment by finding them in the Visual Tree. This will basically give you all Visible DataGridRows and probably a few more because of the Virtualization used in the DataGrid

Example

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


The way I've done it is by hooking up to the DataGrid's LoadingRow and UnloadingRow events.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜