// Copyright (C) Microsoft Corporation. All Rights Reserved. // This code released under the terms of the Microsoft Public License // (Ms-PL, http://opensource.org/licenses/ms-pl.html). using System.Diagnostics.CodeAnalysis; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; namespace Delay { /// /// Class containing a replacement for ToolTipService.SetToolTip that works /// around a Windows 8 platform bug where NullReferenceException is thrown /// from native code the next time a ToolTip is displayed if its Binding /// transitions from non-null to null while on screen. /// public static class ToolTipServiceExtensions { /// /// Gets the value of the ToolTipServiceExtensions.ToolTip XAML attached property for an object. /// /// The object from which the property value is read. /// The object's tooltip content. [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0", Justification = "Underlying method will validate.")] public static object GetToolTip(DependencyObject obj) { return (object)obj.GetValue(ToolTipProperty); } /// /// Sets the value of the ToolTipServiceExtensions.ToolTip XAML attached property. /// /// The object to set tooltip content on. /// The value to set for tooltip content. [SuppressMessage("Microsoft.Design", "CA1062:Validate arguments of public methods", MessageId = "0", Justification = "Underlying method will validate.")] public static void SetToolTip(DependencyObject obj, object value) { obj.SetValue(ToolTipProperty, value); } /// /// Gets or sets the object or string content of an element's ToolTip. /// [SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "Stardard attached property implementation.")] public static readonly DependencyProperty ToolTipProperty = DependencyProperty.RegisterAttached( "ToolTip", typeof(object), typeof(ToolTipServiceExtensions), new PropertyMetadata(null, ToolTipPropertyChangedCallback)); /// /// Method called when the value of an element's ToolTipServiceExtensions.ToolTip XAML attached property changes. /// /// Element for which the property changed. /// Event arguments. private static void ToolTipPropertyChangedCallback(DependencyObject element, DependencyPropertyChangedEventArgs args) { // Capture the new value var newValue = args.NewValue; // Create a ToolTip instance to display the new value var toolTip = new ToolTip { Content = newValue }; // Hide the ToolTip instance if the new value is null // (Prevents the display of a small white rectangle) if (null == newValue) { toolTip.Visibility = Visibility.Collapsed; } // Defer to ToolTipService.SetToolTip for the actual implementation ToolTipService.SetToolTip(element, toolTip); } } }