private static void IsOpen_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e) { ToolTip toolTip = (ToolTip)d; if (e.NewValue is bool) { bool isOpen = (bool)e.NewValue; if (isOpen) { bool wasPopupAlreadyOpen = (toolTip._parentPopup != null && toolTip._parentPopup.IsOpen == true); if (!wasPopupAlreadyOpen) // Note: this prevents loops due to the fact that when the popup opens, the "ToolTip.IsOpen_Changed" method is called because the tooltip is the child of the Popup so its properties are called when it is loaded into the Visual Tree. { // Propagate the DataContext: if (toolTip.INTERNAL_ElementToWhichThisToolTipIsAssigned is FrameworkElement) { toolTip.DataContext = ((FrameworkElement)toolTip.INTERNAL_ElementToWhichThisToolTipIsAssigned).DataContext; } // Make sure the tooltip is transparent to clicks: toolTip.IsHitTestVisible = false; // Make sure the tooltip is Top/Left-aligned: toolTip.HorizontalAlignment = HorizontalAlignment.Left; toolTip.VerticalAlignment = VerticalAlignment.Top; // Create the popup if not already created: if (toolTip._parentPopup == null) { toolTip._parentPopup = new Popup() { Child = toolTip, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Top, HorizontalContentAlignment = HorizontalAlignment.Left, VerticalContentAlignment = VerticalAlignment.Top, }; // Make sure that the popup is displayed in the same Window as the element to which the ToolTip is assigned. This is useful when there are multiple Windows: if (toolTip.INTERNAL_ElementToWhichThisToolTipIsAssigned != null) { toolTip._parentPopup.INTERNAL_ParentWindow = toolTip.INTERNAL_ElementToWhichThisToolTipIsAssigned.INTERNAL_ParentWindow; } } // Calculate the popup position: Point popupAbsolutePosition; if (toolTip._forceSpecifyAbsoluteCoordinates.HasValue) { popupAbsolutePosition = toolTip._forceSpecifyAbsoluteCoordinates.Value; } else { popupAbsolutePosition = INTERNAL_PopupsManager.CalculatePopupAbsolutePositionBasedOnElementPosition( toolTip.INTERNAL_ElementToWhichThisToolTipIsAssigned, toolTip.HorizontalOffset, toolTip.VerticalOffset); } // Set the popup position: toolTip._parentPopup.HorizontalOffset = popupAbsolutePosition.X; toolTip._parentPopup.VerticalOffset = popupAbsolutePosition.Y; // Ensure that the popup stays within the screen bounds if its content is big: toolTip._parentPopup.Loaded -= toolTip._parentPopup_Loaded; // We unregister the event to ensure that it is not registered twice. toolTip._parentPopup.Loaded += toolTip._parentPopup_Loaded; // Open the popup: toolTip._parentPopup.IsOpen = true; //Start the timer to close the popup after 5 seconds: toolTip._timerForClosingTooltipAfter5Seconds.Tick -= toolTip._timerForClosingTooltipAfter5Seconds_Tick; toolTip._timerForClosingTooltipAfter5Seconds.Tick += toolTip._timerForClosingTooltipAfter5Seconds_Tick; toolTip._timerForClosingTooltipAfter5Seconds.Start(); // Raise the "Opened" event: if (toolTip.Opened != null) { toolTip.Opened(toolTip, new RoutedEventArgs()); } } } else { toolTip._timerForClosingTooltipAfter5Seconds.Stop(); if (toolTip._parentPopup != null && toolTip._parentPopup.IsOpen == true) { toolTip._parentPopup.IsOpen = false; // Raise the "Closed" event: if (toolTip.Closed != null) { toolTip.Closed(toolTip, new RoutedEventArgs()); } } } } }
private static void IsOpen_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e) { ToolTip toolTip = (ToolTip)d; bool isOpen = (bool)e.NewValue; if (isOpen) { bool wasPopupAlreadyOpen = (toolTip._parentPopup != null && toolTip._parentPopup.IsOpen == true); // Note: this prevents loops due to the fact that when the popup opens, the "ToolTip.IsOpen_Changed" method is called // because the tooltip is the child of the Popup so its properties are called when it is loaded into the Visual Tree. if (wasPopupAlreadyOpen) { return; } // Make sure the tooltip is transparent to clicks: toolTip.IsHitTestVisible = false; // Make sure the tooltip is Top/Left-aligned: toolTip.HorizontalAlignment = HorizontalAlignment.Left; toolTip.VerticalAlignment = VerticalAlignment.Top; // Create the popup if not already created: if (toolTip._parentPopup == null) { toolTip._parentPopup = new Popup() { Child = toolTip, HorizontalAlignment = HorizontalAlignment.Left, VerticalAlignment = VerticalAlignment.Top, HorizontalContentAlignment = HorizontalAlignment.Left, VerticalContentAlignment = VerticalAlignment.Top, }; toolTip._parentPopup.DataContext = toolTip._owner?.DataContext; toolTip._parentPopup.Loaded += new RoutedEventHandler(ParentPopupLoaded); } Point position = GetMousePosition(); position.X = Math.Max(position.X + toolTip.HorizontalOffset, 0.0) + 10.0; position.Y = Math.Max(position.Y + toolTip.VerticalOffset, 0.0) + 10.0; toolTip._parentPopup.HorizontalOffset = position.X; toolTip._parentPopup.VerticalOffset = position.Y; toolTip._parentPopup.IsOpen = true; if (toolTip.Opened != null) { toolTip.Opened(toolTip, new RoutedEventArgs()); } } else { if (toolTip._parentPopup != null && toolTip._parentPopup.IsOpen == true) { toolTip._parentPopup.IsOpen = false; // Raise the "Closed" event: if (toolTip.Closed != null) { toolTip.Closed(toolTip, new RoutedEventArgs()); } } } }