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