private static void OnShowingKeyTipChanged(DependencyObject element, DependencyPropertyChangedEventArgs e) { if ((bool)e.NewValue) { UIElement uie = RibbonHelper.GetContainingUIElement(element); if (uie != null && uie.Visibility != Visibility.Visible) { // revert the value if the element is not visible element.SetValue(ShowingKeyTipProperty, false); return; } // Raise the ActivatingKeyTip event. ActivatingKeyTipEventArgs activatingEventArgs = new ActivatingKeyTipEventArgs(); IInputElement inputElement = element as IInputElement; if (inputElement != null) { inputElement.RaiseEvent(activatingEventArgs); } // KeyTips could have been dismissed due to one // of the event handler, hence check again. KeyTipService current = Current; if (current.State != KeyTipState.None) { if (activatingEventArgs.KeyTipVisibility == Visibility.Visible) { // Create the keytip and add it as the adorner. UIElement adornedElement = RibbonHelper.GetContainingUIElement(activatingEventArgs.PlacementTarget == null ? element : activatingEventArgs.PlacementTarget); if (adornedElement != null && adornedElement.IsVisible) { bool isScrollAdornerLayer = false; AdornerLayer adornerLayer = GetAdornerLayer(adornedElement, out isScrollAdornerLayer); if (adornerLayer != null) { KeyTipAdorner adorner = new KeyTipAdorner(adornedElement, activatingEventArgs.PlacementTarget, activatingEventArgs.KeyTipHorizontalPlacement, activatingEventArgs.KeyTipVerticalPlacement, activatingEventArgs.KeyTipHorizontalOffset, activatingEventArgs.KeyTipVerticalOffset, activatingEventArgs.Handled ? null : activatingEventArgs.OwnerRibbonGroup); LinkKeyTipControlToAdorner(adorner, element); adornerLayer.Add(adorner); element.SetValue(KeyTipAdornerProperty, adorner); element.SetValue(KeyTipAdornerHolderProperty, adornedElement); // Begin invode an operation to nudge all the keytips into the // adorner layer boundary unless the layer belongs to a scroll viewer. if (!isScrollAdornerLayer) { current.EnqueueAdornerLayerForPlacementProcessing(adornerLayer); } } } } if (activatingEventArgs.KeyTipVisibility != Visibility.Collapsed) { // add the element to currentActiveKeyTipElement list. current._currentActiveKeyTipElements.Add(element); } else { // Revert the value if it is asked by event handlers // (i.e, by setting KeyTipVisibility to collapsed. element.SetValue(ShowingKeyTipProperty, false); } } else { // Revert the value if we already dismissed keytips. element.SetValue(ShowingKeyTipProperty, false); } } else { // Remove keytip from adorner. KeyTipAdorner adorner = (KeyTipAdorner)element.GetValue(KeyTipAdornerProperty); UIElement adornedElement = (UIElement)element.GetValue(KeyTipAdornerHolderProperty); if (adornedElement != null && adorner != null) { UnlinkKeyTipControlFromAdorner(adorner); bool isScrollAdornerLayer = false; AdornerLayer adornerLayer = GetAdornerLayer(adornedElement, out isScrollAdornerLayer); if (adornerLayer != null) { adornerLayer.Remove(adorner); } } element.ClearValue(KeyTipAdornerProperty); element.ClearValue(KeyTipAdornerHolderProperty); } }
private static void UnlinkKeyTipControlFromAdorner(KeyTipAdorner adorner) { KeyTipControl keyTipControl = adorner.KeyTipControl; adorner.UnlinkKeyTipControl(); if (keyTipControl != null) { // Save the unlinked KeyTipControl to cache for reuse. KeyTipService current = Current; if (current._cachedKeyTipControls == null) { current._cachedKeyTipControls = new List<KeyTipControl>(); } current._cachedKeyTipControls.Add(keyTipControl); } }
private static void LinkKeyTipControlToAdorner(KeyTipAdorner adorner, DependencyObject keyTipElement) { KeyTipService current = Current; KeyTipControl keyTipControl = null; if (current._cachedKeyTipControls != null && current._cachedKeyTipControls.Count > 0) { // Retrieve a KeyTipControl from cache for reuse. int count = current._cachedKeyTipControls.Count; keyTipControl = current._cachedKeyTipControls[count - 1]; current._cachedKeyTipControls.RemoveAt(count - 1); } if (keyTipControl == null) { keyTipControl = new KeyTipControl(); } adorner.LinkKeyTipControl(keyTipElement, keyTipControl); }