/* * private static Visual GetNearestVisual(object value) * { * Visual visual = null; * if (value != null) * { * visual = value as Visual; * if (visual == null) * { * ContentElement ce = value as ContentElement; * if (ce != null) * { * visual = ce.GetUIParent() as Visual; * } * } * } * return visual; * } */ private static DependencyObject GetRoot(DependencyObject element) { if (element == null) { return(null); } DependencyObject parent = null; DependencyObject dependencyObject = element; ContentElement ce = element as ContentElement; if (ce != null) { dependencyObject = ce.GetUIParent(); } while (dependencyObject != null) { parent = dependencyObject; dependencyObject = VisualTreeHelper.GetParent(dependencyObject); } return(parent); }
private static DependencyObject GetParentScope(DependencyObject childScope) { // Get the parent element of the childScope element DependencyObject parent = null; UIElement element = childScope as UIElement; ContentElement contentElement = (element == null) ? childScope as ContentElement : null; UIElement3D element3D = (element == null && contentElement == null) ? childScope as UIElement3D : null; if (element != null) { parent = element.GetUIParent(true); } else if (contentElement != null) { parent = contentElement.GetUIParent(true); } else if (element3D != null) { parent = element3D.GetUIParent(true); } if (parent != null) { // Get the next focus scope above this one return(FocusManager.GetFocusScope(parent)); } return(null); }
internal static void InvalidateAutomationAncestors(DependencyObject o) { UIElement e = null; UIElement3D e3d = null; ContentElement ce = null; Stack <DependencyObject> branchNodeStack = new Stack <DependencyObject>(); bool continueInvalidation = true; while (o != null && continueInvalidation) { continueInvalidation &= InvalidateAutomationPeer(o, out e, out ce, out e3d); // // Invoke InvalidateAutomationAncestorsCore // bool continuePastVisualTree = false; if (e != null) { continueInvalidation &= e.InvalidateAutomationAncestorsCore(branchNodeStack, out continuePastVisualTree); // Get element's visual parent o = e.GetUIParent(continuePastVisualTree); } else if (ce != null) { continueInvalidation &= ce.InvalidateAutomationAncestorsCore(branchNodeStack, out continuePastVisualTree); // Get element's visual parent o = (DependencyObject)ce.GetUIParent(continuePastVisualTree); } else if (e3d != null) { continueInvalidation &= e3d.InvalidateAutomationAncestorsCore(branchNodeStack, out continuePastVisualTree); // Get element's visual parent o = e3d.GetUIParent(continuePastVisualTree); } } }
// 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); }
internal IInputElement FindTarget(PresentationSource inputSource, Point position) { IInputElement stylusOver = null; switch (_captureMode) { case CaptureMode.None: { stylusOver = StylusDevice.GlobalHitTest(inputSource, position); // We understand UIElements and ContentElements. // If we are over something else (like a raw visual) // find the containing element. if (!InputElement.IsValid(stylusOver)) { stylusOver = InputElement.GetContainingInputElement(stylusOver as DependencyObject); } } break; case CaptureMode.Element: stylusOver = _stylusCapture; break; case CaptureMode.SubTree: { IInputElement stylusCapture = InputElement.GetContainingInputElement(_stylusCapture as DependencyObject); if (stylusCapture != null && inputSource != null) { // We need to re-hit-test to get the "real" UIElement we are over. // This allows us to have our capture-to-subtree span multiple windows. // GlobalHitTest always returns an IInputElement, so we are sure to have one. stylusOver = StylusDevice.GlobalHitTest(inputSource, position); } if (stylusOver != null && !InputElement.IsValid(stylusOver)) { stylusOver = InputElement.GetContainingInputElement(stylusOver as DependencyObject); } // Make sure that the element we hit is acutally underneath // our captured element. Because we did a global hit test, we // could have hit an element in a completely different window. // // Note that we support the child being in a completely different window. // So we use the GetUIParent method instead of just looking at // visual/content parents. if (stylusOver != null) { IInputElement ieTest = stylusOver; UIElement eTest = null; ContentElement ceTest = null; while (ieTest != null && ieTest != stylusCapture) { eTest = ieTest as UIElement; if (eTest != null) { ieTest = InputElement.GetContainingInputElement(eTest.GetUIParent(true)); } else { ceTest = ieTest as ContentElement; // Should never fail. ieTest = InputElement.GetContainingInputElement(ceTest.GetUIParent(true)); } } // If we missed the capture point, we didn't hit anything. if (ieTest != stylusCapture) { stylusOver = _stylusCapture; } } else { // We didn't hit anything. Consider the stylus over the capture point. stylusOver = _stylusCapture; } } break; } return(stylusOver); }
private IInputElement CriticalHitTest(Point point, bool isSynchronize) { IInputElement over = null; if (_activeSource != null) { switch (_captureMode) { case CaptureMode.None: // No capture, do a regular hit-test. if (_isDown) { if (isSynchronize) { // In a synchronize call, we need to hit-test the window in addition to the element over = GlobalHitTest(point, _activeSource); } else { // Just hit-test the element over = LocalHitTest(point, _activeSource); } EnsureValid(ref over); } break; case CaptureMode.Element: // Capture is to a specific element, so the device will always be over that element. over = _captured; break; case CaptureMode.SubTree: // Capture is set to an entire subtree. Hit-test to determine the element (and window) // the device is over. If the element is within the captured sub-tree (which can span // multiple windows), then the device is over that element. If the element is not within // the sub-tree, then the device is over the captured element. { IInputElement capture = InputElement.GetContainingInputElement(_captured as DependencyObject); if (capture != null) { // We need to re-hit-test to get the "real" UIElement we are over. // This allows us to have our capture-to-subtree span multiple windows. // GlobalHitTest always returns an IInputElement, so we are sure to have one. over = GlobalHitTest(point, _activeSource); } EnsureValid(ref over); // Make sure that the element we hit is acutally underneath // our captured element. Because we did a global hit test, we // could have hit an element in a completely different window. // // Note that we support the child being in a completely different window. // So we use the GetUIParent method instead of just looking at // visual/content parents. if (over != null) { IInputElement ieTest = over; while ((ieTest != null) && (ieTest != _captured)) { UIElement eTest = ieTest as UIElement; if (eTest != null) { ieTest = InputElement.GetContainingInputElement(eTest.GetUIParent(true)); } else { ContentElement ceTest = ieTest as ContentElement; if (ceTest != null) { ieTest = InputElement.GetContainingInputElement(ceTest.GetUIParent(true)); } else { UIElement3D e3DTest = (UIElement3D)ieTest; ieTest = InputElement.GetContainingInputElement(e3DTest.GetUIParent(true)); } } } if (ieTest != _captured) { // If we missed the capture point, consider the device over the capture point. over = _captured; } } else { // If we didn't hit anything, consider the device over the capture point. over = _captured; } } break; } } return(over); }