views:

893

answers:

1

I am using a PagedCollectionView in Silverlight 3 to group items in a datagrid. I want to detect when the group headers are clicked but after 6 hours still cannot find any way to do this.

(So that when a collapsed header is clicked I can dynamically load the group's content)

The datagrid is populated like so:

PagedCollectionView collection = new PagedCollectionView(orgMembers); collection.GroupDescriptions.Add(new PropertyGroupDescription("Generation"));

DataGrid1.ItemsSource = collection;

+4  A: 

write an extension method to find a parent element of a specific type:

    public static T FindParentOfType<T>(this FrameworkElement element)
    {
        var parent = VisualTreeHelper.GetParent(element) as FrameworkElement;

        while (parent != null)
        {
            if (parent is T)
                return (T)(object)parent;

            parent = VisualTreeHelper.GetParent(parent) as FrameworkElement;
        }
        return default(T);
    }

Handle the MouseLeftButtonUp event on the datagrid:

    private void PassportGrid_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
    {

        DataGridRowGroupHeader rgh = (e.OriginalSource as FrameworkElement).FindParentOfType<DataGridRowGroupHeader>();
        if (rgh != null && rgh.DataContext is CollectionViewGroup)
        {
            var stuff = (rgh.DataContext as CollectionViewGroup);
            var items = stuff.Items;
        }
    }

you can get info on the group that was clicked and its item collection (shown above)

Kevin