예제 #1
0
            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;
                }
            }
예제 #2
0
        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);
            }
        }
예제 #3
0
        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);
                                }
                            }
                        }
                    }
                }
            }
        }
예제 #4
0
        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);
            }
        }