static void ShowFocusVisual(Control control, FrameworkElement target) { HideFocusVisual(); AdornerLayer adornerlayer = AdornerLayer.GetAdornerLayer(target); if (adornerlayer == null) { return; } Style fvs = target.FocusVisualStyle; if (fvs != null && fvs.BasedOn == null && fvs.Setters.Count == 0) { fvs = target.TryFindResource(SystemParameters.FocusVisualStyleKey) as Style; } if (fvs != null) { _focusVisualAdornerCache = new FocusVisualAdorner(control, target, fvs); adornerlayer.Add(_focusVisualAdornerCache); // Hide the focus visual when IsVisible changes to avoid an internal WPF exception control.IsVisibleChanged += OnControlIsVisibleChanged; } }
private void OnTargetChanged(object sender, EventArgs e) { if (focusVisualAdorner != null) { focusVisualAdorner.Dispose(); focusVisualAdorner = null; } FrameworkElement focusedElement = (FrameworkElement)presentationSource.KeyboardDevice.Target; if (focusedElement != null) { focusVisualAdorner = FocusVisualAdorner.Attach(focusedElement); SetNavigationFocusElement(focusedElement); } }
private void ShowFocusVisual(DependencyObject element) { // Always hide the existing focus visual HideFocusVisual(); // Disable keyboard cues (accesskey underline) if keyboard device is not MostRecentInputDevice if (!IsKeyboardMostRecentInputDevice()) { EnableKeyboardCues(element, false); } // Show focus visual if system metric is true or keyboard is used last if (AlwaysShowFocusVisual || IsKeyboardMostRecentInputDevice()) { FrameworkElement fe = element as FrameworkElement; if (fe != null) { AdornerLayer adornerlayer = AdornerLayer.GetAdornerLayer(fe); if (adornerlayer == null) return; Style fvs = fe.FocusVisualStyle; // WORKAROUND: (Bug 1016350) If FocusVisualStyle is the "default" value // then we load the default FocusVisualStyle from ResourceDictionary. if (fvs == FrameworkElement.DefaultFocusVisualStyle) { fvs = SystemResources.FindResourceInternal(SystemParameters.FocusVisualStyleKey) as Style; } if (fvs != null) { _focusVisualAdornerCache = new FocusVisualAdorner(fe, fvs); adornerlayer.Add(_focusVisualAdornerCache); } } else // If not FrameworkElement { FrameworkContentElement fce = element as FrameworkContentElement; if (fce != null) { IContentHost parentICH = null; UIElement parentUIElement = GetParentUIElementFromContentElement(fce, ref parentICH); if (parentICH != null && parentUIElement != null) { AdornerLayer adornerlayer = AdornerLayer.GetAdornerLayer(parentUIElement); if (adornerlayer != null) { Style fvs = fce.FocusVisualStyle; // WORKAROUND: (Bug 1016350) If FocusVisualStyle is the "default" value // then we load the default FocusVisualStyle from ResourceDictionary. if (fvs == FrameworkElement.DefaultFocusVisualStyle) { fvs = SystemResources.FindResourceInternal(SystemParameters.FocusVisualStyleKey) as Style; } if (fvs != null) { _focusVisualAdornerCache = new FocusVisualAdorner(fce, parentUIElement, parentICH, fvs); adornerlayer.Add(_focusVisualAdornerCache); } } } } } } }