protected override Size MeasureOverride(Size availableSize) { Panel panel = this.GroupLevelIndicatorPaneHost; if (panel == null) { return(base.MeasureOverride(availableSize)); } DataGridContext dataGridContext = DataGridControl.GetDataGridContext(this); if (dataGridContext != null) { ObservableCollection <GroupDescription> groupDescriptions = DataGridContext.GetGroupDescriptionsHelper(dataGridContext.Items); int leafGroupLevel = GroupLevelIndicatorPane.GetGroupLevel(this); // If Indented is true (default), we use the total groupDescriptions.Count for this DataGridContext int correctedGroupLevel = (this.Indented == true) ? groupDescriptions.Count : leafGroupLevel; // Ensure that the GroupLevel retrieved does not exceeds the number of group descriptions for the DataGridContext correctedGroupLevel = Math.Min(correctedGroupLevel, groupDescriptions.Count); // Then finally, if the GroupLevel is -1, then indent at maximum. if (correctedGroupLevel == -1) { correctedGroupLevel = groupDescriptions.Count; } if ((correctedGroupLevel > 0) && (this.AreGroupsFlattened)) { correctedGroupLevel = (this.Indented) ? 1 : 0; } UIElementCollection children = panel.Children; int childrenCount = children.Count; // If we need to add/remove GroupLevelIndicators from the panel if (correctedGroupLevel != childrenCount) { // When grouping change, we take for granted that the group deepness will change, // so we initialize DataContext of the margin only in there. // Clear all the panel's children! children.Clear(); // Create 1 group margin content presenter for each group level for (int i = correctedGroupLevel - 1; i >= 0; i--) { GroupLevelIndicator groupMargin = new GroupLevelIndicator(); groupMargin.DataContext = dataGridContext.GroupLevelDescriptions[i]; children.Insert(0, new GroupLevelIndicator()); } childrenCount = correctedGroupLevel; this.SetCurrentIndicatorCount(childrenCount); } object item = dataGridContext.GetItemFromContainer(this); for (int i = 0; i < childrenCount; i++) { GroupLevelIndicator groupMargin = children[i] as GroupLevelIndicator; CollectionViewGroup groupForIndicator = GroupLevelIndicatorPane.GetCollectionViewGroupHelper( dataGridContext, groupDescriptions, item, i); GroupConfiguration groupLevelConfig = GroupConfiguration.GetGroupConfiguration( dataGridContext, groupDescriptions, dataGridContext.GroupConfigurationSelector, i, groupForIndicator); if (groupLevelConfig != null) { Binding groupLevelIndicatorStyleBinding = BindingOperations.GetBinding(groupMargin, GroupLevelIndicator.StyleProperty); if ((groupLevelIndicatorStyleBinding == null) || (groupLevelIndicatorStyleBinding.Source != groupLevelConfig)) { groupLevelIndicatorStyleBinding = new Binding("GroupLevelIndicatorStyle"); groupLevelIndicatorStyleBinding.Source = groupLevelConfig; groupMargin.SetBinding(GroupLevelIndicator.StyleProperty, groupLevelIndicatorStyleBinding); } } else { groupMargin.ClearValue(GroupLevelIndicator.StyleProperty); } // If the ShowIndicators property is False or there is already leafGroupLevel GroupLevelIndicators in the panel, // the current newGroupMargin must be hidden. if ((!GroupLevelIndicatorPane.GetShowIndicators(this)) || ((i >= leafGroupLevel) && (leafGroupLevel != -1))) { groupMargin.Visibility = Visibility.Hidden; } else { groupMargin.Visibility = Visibility.Visible; } } } return(base.MeasureOverride(availableSize)); }