private static object OnCoerceGroupChanged(DependencyObject o, object newValue) { ForeignKeyGroupContentControl contentControl = o as ForeignKeyGroupContentControl; if (contentControl != null) { Group currentGroup = contentControl.Group; Group newGroup = newValue as Group; bool clearContentTemplate = false; if ((currentGroup != null) && (newGroup != null)) { if ((currentGroup.Level == -1) || (newGroup.Level == -1)) { clearContentTemplate = true; } // The Group is different, must clear the templates else if (currentGroup.GroupBy != newGroup.GroupBy) { clearContentTemplate = true; } } else { // The Group is null or different, must clear the templates clearContentTemplate = true; } if (clearContentTemplate) { contentControl.ClearValue(ContentControl.ContentTemplateProperty); contentControl.ClearValue(ContentControl.ContentTemplateSelectorProperty); } } return(newValue); }