RadGridView WPF always expands when updating the ItemsSource

1 Answer 23 Views
GridView
Andreea
Top achievements
Rank 1
Iron
Andreea asked on 01 Mar 2024, 10:03 AM | edited on 11 Mar 2024, 02:15 PM

Hi,

I have a RadGridView in WPF where I use CustomGrouping for displaying it as a hierarchical structure. I set on it AutoExpandGroups="True" in order to be expanded by default. My problem is, if I manually collapse something in the RadGridView and I make an update to the list bounded to ItemsSource, the RadGridView automatically expands and does not stay as it was.

Thanks

Andreea
Top achievements
Rank 1
Iron
commented on 12 Mar 2024, 10:55 AM

This is the closest I could get but it still does not work.

private Dictionary<object, GroupHeaderRow> groupRowMapping = new Dictionary<object, GroupHeaderRow>();
private List<object> collapsedGroupKeys = new List<object>();

private void radGridView_Loaded(object sender, RoutedEventArgs e)
{
    ReloadData();
}

private void radGridView_OnGroupRowIsExpandedChanged(object sender, GroupRowEventArgs e)
{
    if (!(e.Row is GroupHeaderRow groupHeaderRow))
        return;

    var groupKey = DetermineGroupKey(groupHeaderRow);

    if (groupKey != null)
    {
        if (!groupHeaderRow.IsExpanded)
        {
            collapsedGroupKeys.Add(groupKey);
        }
        else
        {
            collapsedGroupKeys.Remove(groupKey);
        }
    }
}
private object DetermineGroupKey(GroupHeaderRow groupHeaderRow)
{
    return groupHeaderRow.DataContext; 
}

private void ReloadData()
{
    PopulateGroupRowMapping();
    RestoreCollapsedState();
}

private void RestoreCollapsedState()
{
    foreach (var groupRowKey in collapsedGroupKeys)
    {
        if (groupRowMapping.ContainsKey(groupRowKey))
        {
            var groupRow = groupRowMapping[groupRowKey];
            groupRow.IsExpanded = false;
        }
    }
}

private void PopulateGroupRowMapping()
{
    groupRowMapping.Clear();
    if (radGridView.Items.Groups != null)
    {
        foreach (var groupRow in radGridView.Items.Groups)
        {
            if (groupRow is CollectionViewGroup collectionViewGroup)
            {
                foreach (var item in collectionViewGroup.Items)
                {
                    if (item is GroupHeaderRow headerRow)
                    {
                        var groupKey = DetermineGroupKey(headerRow);
                        if (groupKey != null && !groupRowMapping.ContainsKey(groupKey))
                        {
                            groupRowMapping[groupKey] = headerRow;
                        }
                    }
                }
            }
        }
    }
}

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 14 Mar 2024, 09:02 AM

Hello Andreea,

If you make an update in the existing list assigned to the ItemsSource the groups should not change their expand state. Do you mean that you reset the ItemsSource property? If that is the case, then you can consider only removing and adding items from the existing collection instance. Otherwise, you will need to cache the IsExpanded state of the groups and restore it manually similar to what you do in your custom code with the "groupRowMapping" dictionary.

Regards,
Martin Ivanov
Progress Telerik

A brand new ThemeBuilder course was just added to the Virtual Classroom. The training course was designed to help you get started with ThemeBuilder for styling Telerik and Kendo UI components for your applications. You can check it out at https://learn.telerik.com
Andreea
Top achievements
Rank 1
Iron
commented on 14 Mar 2024, 12:33 PM

Thank you, Martin!

Yes, I cached the IsExpanded state.

Tags
GridView
Asked by
Andreea
Top achievements
Rank 1
Iron
Answers by
Martin Ivanov
Telerik team
Share this question
or