public static Visual GetDescendantByName(FrameworkElement element, string name) { if (element == null) { return null; } if (element.Name == name) { return element; } Visual descendantByName = null; if (element != null) { element.ApplyTemplate(); } for (int i = 0; i < VisualTreeHelper.GetChildrenCount(element); i++) { Visual child = VisualTreeHelper.GetChild(element, i) as Visual; descendantByName = GetDescendantByName((FrameworkElement) child, name); if (descendantByName != null) { return descendantByName; } } return descendantByName; }
public static void ApplyAllTemplates(this sw.FrameworkElement parent) { if (parent == null) { return; } parent.ApplyTemplate(); foreach (var logicalChild in sw.LogicalTreeHelper.GetChildren(parent)) { if (logicalChild is sw.FrameworkElement childElement) { // recursively apply childElement.ApplyAllTemplates(); } } int childrenCount = swm.VisualTreeHelper.GetChildrenCount(parent); for (int i = 0; i < childrenCount; i++) { var child = swm.VisualTreeHelper.GetChild(parent, i); // If the child is not of the request child type child if (child is sw.FrameworkElement childElement) { // recursively apply childElement.ApplyAllTemplates(); } } }
public static void OpenParentRibbonGroupDropDownSync(FrameworkElement fe, bool templateApplied) { if (!templateApplied) { // Apply template if not yet applied. fe.ApplyTemplate(); } // Get the Parent RibbonGroup and open its dropdown if needed. RibbonGroup ribbonGroup = TreeHelper.FindAncestor(fe, delegate(DependencyObject element) { return (element is RibbonGroup); }) as RibbonGroup; if (ribbonGroup == null) { ribbonGroup = TreeHelper.FindLogicalAncestor<RibbonGroup>(fe); } if (ribbonGroup != null && ribbonGroup.IsCollapsed && !ribbonGroup.IsDropDownOpen) { ribbonGroup.IsDropDownOpen = true; fe.UpdateLayout(); } }