internal static DependencyObject GetUIParentCore(DependencyObject o) { UIElement e = o as UIElement; if (e != null) { return(e.GetUIParentCore()); } else { ContentElement ce = o as ContentElement; if (ce != null) { return(ce.GetUIParentCore()); } else { UIElement3D e3D = o as UIElement3D; if (e3D != null) { return(e3D.GetUIParentCore()); } } return(null); } }
internal static DependencyObject GetUIParent(DependencyObject child, bool continuePastVisualTree) { DependencyObject parent = null; DependencyObject myParent = null; // Try to find a UIElement parent in the visual ancestry. if (child is Visual) { myParent = ((Visual)child).InternalVisualParent; } else { myParent = ((Visual3D)child).InternalVisualParent; } parent = InputElement.GetContainingUIElement(myParent) as DependencyObject; // If there was no UIElement parent in the visual ancestry, // check along the logical branch. if (parent == null && continuePastVisualTree) { UIElement childAsUIElement = child as UIElement; if (childAsUIElement != null) { parent = InputElement.GetContainingInputElement(childAsUIElement.GetUIParentCore()) as DependencyObject; } else { UIElement3D childAsUIElement3D = child as UIElement3D; if (childAsUIElement3D != null) { parent = InputElement.GetContainingInputElement(childAsUIElement3D.GetUIParentCore()) as DependencyObject; } } } return(parent); }
// Walk up the parent chain to find the closest element with IsFocusScope=true private static DependencyObject _GetFocusScope(DependencyObject d) { if (d == null) { return(null); } if ((bool)d.GetValue(IsFocusScopeProperty)) { return(d); } // Step 1: Walk up the logical tree UIElement uiElement = d as UIElement; if (uiElement != null) { DependencyObject logicalParent = uiElement.GetUIParentCore(); if (logicalParent != null) { return(GetFocusScope(logicalParent)); } } else { ContentElement ce = d as ContentElement; if (ce != null) { DependencyObject logicalParent = ce.GetUIParent(true); if (logicalParent != null) { return(_GetFocusScope(logicalParent)); } } else { UIElement3D uiElement3D = d as UIElement3D; if (uiElement3D != null) { DependencyObject logicalParent = uiElement3D.GetUIParentCore(); if (logicalParent != null) { return(GetFocusScope(logicalParent)); } } } } // Step 2: Walk up the visual tree if (d is Visual || d is Visual3D) { DependencyObject visualParent = VisualTreeHelper.GetParent(d); if (visualParent != null) { return(_GetFocusScope(visualParent)); } } // If visual and logical parent is null - then the element is implicit focus scope return(d); }