private static void HandlePreviewGotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e) { if (FocusScopeManager.Instance.ShouldDenyFocusChange) { e.Handled = true; return; } DependencyObject newFocusObject = e.NewFocus as DependencyObject; // if there is a newly focused object and it is not allowed focus if (newFocusObject != null && !FocusScopeManager.GetAllowedFocus(newFocusObject)) { // Push focus back to the client's desired focus sink and cancel the change // of focus to new focus. FocusScopeManager.Instance.ReturnFocus(); e.Handled = true; } }
private object CoerceFocusedElement(object newFocus) { UIElement newFocusElement = newFocus as UIElement; if (newFocusElement != null) { // If we have been told to deny the next focus change then do so if (this.ShouldDenyFocusChange) { this.EndDenyNextFocusChange(); return(DependencyProperty.UnsetValue); } // Don't allow logical focus to go to elements that are not focusable. if (newFocus != null && !FocusScopeManager.GetAllowedFocus(newFocusElement)) { return(DependencyProperty.UnsetValue); } } return(newFocus); }