user626528
user626528

Reputation: 14419

How to find visible DataGrid rows in Silverlight?

How to find visible DataGrid rows in Silverlight?

Upvotes: 2

Views: 2317

Answers (2)

Samuel Jack
Samuel Jack

Reputation: 33270

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

Upvotes: 0

Fredrik Hedblad
Fredrik Hedblad

Reputation: 84657

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

Upvotes: 1

Related Questions