private void OnScopeKeyboardFocusChanged(UIElement focusScope, int priority) { switch (this.ActivateBehavior) { case FocusScopeManagerActivateBehavior.ClearFocusedElementInLowerPriorityScopes: if (focusScope == this.ActiveManagedFocusScope) { break; } int indexForPriority = this.FindStartIndexForPriority(priority); if (!this.IsFocusScopeManaged(focusScope, priority, indexForPriority)) { break; } for (int index = indexForPriority; index < this.scopes.Count; ++index) { WeakReference weakReference = this.scopes[index]; if (weakReference.IsAlive) { UIElement uiElement = (UIElement)weakReference.Target; if (uiElement != focusScope) { FocusManager.SetFocusedElement((DependencyObject)uiElement, (IInputElement)null); } } else { this.listContainsDeadReferences = true; } } this.CleanUpDeadReferences(); this.ActiveManagedFocusScope = focusScope; break; case FocusScopeManagerActivateBehavior.UpdateFocusedElementInAncestorFocusScopes: DependencyObject element = (DependencyObject)focusScope; while (true) { DependencyObject parentFocusScope = FocusScopeManager.GetParentFocusScope(element); if (parentFocusScope != null) { FocusManager.SetFocusedElement(parentFocusScope, (IInputElement)element); element = parentFocusScope; } else { break; } } break; } }
public static void SetFocusToFocusScope(IInputElement newFocus) { UIElement uiElement = newFocus as UIElement; if (uiElement == null) { return; } UIElement focusScope = FocusManager.GetFocusScope((DependencyObject)uiElement) as UIElement; int focusScopePriority = FocusScopeManager.GetFocusScopePriority((DependencyObject)focusScope); if (focusScopePriority == FocusScopeManager.DefaultFocusScopePriority) { return; } FocusScopeManager.Instance.OnScopeKeyboardFocusChanged(focusScope, focusScopePriority); }
private object CoerceFocusedElement(object newFocus) { UIElement uiElement = newFocus as UIElement; if (uiElement != null) { if (this.ShouldDenyFocusChange) { this.EndDenyNextFocusChange(); return(DependencyProperty.UnsetValue); } if (newFocus != null && !FocusScopeManager.GetAllowedFocus((DependencyObject)uiElement)) { return(DependencyProperty.UnsetValue); } } return(newFocus); }
private bool IsFocusScopeManaged(UIElement focusScope, int priority, int priorityStartIndex) { for (int index = priorityStartIndex; index < this.scopes.Count; ++index) { WeakReference weakReference = this.scopes[index]; if (weakReference.IsAlive) { UIElement uiElement = (UIElement)weakReference.Target; if (FocusScopeManager.GetFocusScopePriority((DependencyObject)uiElement) > priority) { return(false); } if (uiElement == focusScope) { return(true); } } } return(false); }
private int FindStartIndexForPriority(int priority) { int index; for (index = 0; index < this.scopes.Count; ++index) { WeakReference weakReference = this.scopes[index]; if (weakReference.IsAlive) { if (FocusScopeManager.GetFocusScopePriority((DependencyObject)weakReference.Target) >= priority) { break; } } else { this.listContainsDeadReferences = true; } } return(index); }
internal static void HandlePreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) { DependencyObject element = e.NewFocus as DependencyObject; Visual visual = element as Visual; if (FocusScopeManager.Instance.ShouldDenyFocusChange) { FocusScopeManager.Instance.EndDenyNextFocusChange(); if (!(visual is ExpressionFloatingWindow)) { e.Handled = true; return; } } if (element == null || FocusScopeManager.GetAllowedFocus(element)) { return; } FocusScopeManager.Instance.ReturnFocus(); e.Handled = true; }
private static void HandleGotKeyboardFocusEvent(object sender, KeyboardFocusChangedEventArgs e) { FocusScopeManager.SetFocusToFocusScope(e.NewFocus); }