// Helper method that ensures that the element marked as IsSelected also has focus. // If it cannot receive focus, we look for its closest visual child that can // private void SynchronizeSelectionFocus(StealFocusMode focusMode) { // Is there something to select? if (_selection == null) { return; } // Are we even allowed to mess around with focus or does someone else on the // design surface have focus right now and we should just let them have it? if (focusMode == StealFocusMode.OnlyIfCategoryListHasFocusWithin && !this.IsKeyboardFocusWithin) { return; } if (focusMode == StealFocusMode.OnlyIfCurrentSelectionDoesNotHaveFocusWithin && _selection.IsKeyboardFocusWithin) { return; } FrameworkElement focusableElement = VisualTreeUtils.FindFocusableElement <FrameworkElement>(_selection); if (focusableElement != null) { focusableElement.Focus(); } }
// Find the closest IsSelectable parent, select it and set focus on it. // private void SelectAndFocus(DependencyObject element, StealFocusMode focusMode) { Select(element); SynchronizeSelectionFocus(focusMode); }