tooltip.cs source code in C# .NET

Source code for the .NET framework in C#

                        

Code:

/ Net / Net / 3.5.50727.3053 / DEVDIV / depot / DevDiv / releases / Orcas / SP / wpf / src / Framework / System / Windows / Controls / tooltip.cs / 1 / tooltip.cs

                            //---------------------------------------------------------------------------- 
//
// Copyright (C) Microsoft Corporation.  All rights reserved.
//
//--------------------------------------------------------------------------- 

using System; 
using System.Diagnostics; 
using System.ComponentModel;
 
using System.Collections;
using System.Collections.Specialized;
using System.Windows.Threading;
 
using System.Windows;
using System.Windows.Media; 
using System.Windows.Input; 
using System.Windows.Data;
using System.Windows.Automation.Peers; 
using System.Windows.Controls.Primitives;
using System.Windows.Markup;
using System.Windows.Shapes;
using MS.Utility; 
using MS.Internal.KnownBoxes;
 
namespace System.Windows.Controls 
{
    ///  
    /// A control to display information when the user hovers over a control
    /// 
    [DefaultEvent("Opened")]
    [Localizability(LocalizationCategory.ToolTip)] 
    public class ToolTip : ContentControl
    { 
        #region Constructors 

        static ToolTip() 
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ToolTip), new FrameworkPropertyMetadata(typeof(ToolTip)));
            _dType = DependencyObjectType.FromSystemTypeInternal(typeof(ToolTip));
            BackgroundProperty.OverrideMetadata(typeof(ToolTip), new FrameworkPropertyMetadata(SystemColors.InfoBrush)); 
            FocusableProperty.OverrideMetadata(typeof(ToolTip), new FrameworkPropertyMetadata(false));
        } 
 
        /// 
        /// Creates a default ToolTip 
        /// 
        public ToolTip() : base()
        {
        } 

        #endregion 
 
        #region Public Properties
 
        /// 
        /// The DependencyProperty for the HorizontalOffset property.
        /// Default: Length(0.0)
        ///  
        public static readonly DependencyProperty HorizontalOffsetProperty =
            ToolTipService.HorizontalOffsetProperty.AddOwner(typeof(ToolTip), 
                            new FrameworkPropertyMetadata(null, 
                                                          new CoerceValueCallback(CoerceHorizontalOffset)));
 
        private static object CoerceHorizontalOffset(DependencyObject d, object value)
        {
            return PopupControlService.CoerceProperty(d, value, ToolTipService.HorizontalOffsetProperty);
        } 

        ///  
        /// Horizontal offset from the default location when this ToolTIp is displayed 
        /// 
        [TypeConverter(typeof(LengthConverter))] 
        [Bindable(true), Category("Layout")]
        public double HorizontalOffset
        {
            get 
            {
                return (double)GetValue(HorizontalOffsetProperty); 
            } 
            set
            { 
                SetValue(HorizontalOffsetProperty, value);
            }
        }
 
        /// 
        /// The DependencyProperty for the VerticalOffset property. 
        /// Default: Length(0.0) 
        /// 
        public static readonly DependencyProperty VerticalOffsetProperty = 
            ToolTipService.VerticalOffsetProperty.AddOwner(typeof(ToolTip),
                            new FrameworkPropertyMetadata(null,
                                                          new CoerceValueCallback(CoerceVerticalOffset)));
 
        private static object CoerceVerticalOffset(DependencyObject d, object value)
        { 
            return PopupControlService.CoerceProperty(d, value, ToolTipService.VerticalOffsetProperty); 
        }
 
        /// 
        /// Vertical offset from the default location when this ToolTip is displayed
        /// 
        [TypeConverter(typeof(LengthConverter))] 
        [Bindable(true), Category("Layout")]
        public double VerticalOffset 
        { 
            get
            { 
                return (double)GetValue(VerticalOffsetProperty);
            }
            set
            { 
                SetValue(VerticalOffsetProperty, value);
            } 
        } 

        ///  
        /// DependencyProperty for the IsOpen property
        /// Default value: false
        /// 
        public static readonly DependencyProperty IsOpenProperty = 
                    DependencyProperty.Register(
                                "IsOpen", 
                                typeof(bool), 
                                typeof(ToolTip),
                                new FrameworkPropertyMetadata( 
                                            false,
                                            FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                                            new PropertyChangedCallback(OnIsOpenChanged)));
 
        private static void OnIsOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        { 
            ToolTip t = (ToolTip) d; 

            if ((bool)e.NewValue) 
            {
                if (t._parentPopup == null)
                {
                    t.HookupParentPopup(); 
                }
            } 
            else 
            {
                // When ToolTip is about to close but still hooked up - we need to raise Accessibility event 
                if (AutomationPeer.ListenerExists(AutomationEvents.ToolTipClosed))
                {
                    AutomationPeer peer = UIElementAutomationPeer.CreatePeerForElement(t);
                    if (peer != null) 
                        peer.RaiseAutomationEvent(AutomationEvents.ToolTipClosed);
                } 
            } 
        }
 
        /// 
        /// Whether or not this ToolTip is visible
        /// 
        [Bindable(true), Browsable(false), Category("Appearance")] 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool IsOpen 
        { 
            get { return (bool) GetValue(IsOpenProperty); }
            set { SetValue(IsOpenProperty, value); } 
        }

        /// 
        ///     The DependencyProperty for HasDropShadow 
        /// 
        public static readonly DependencyProperty HasDropShadowProperty = 
                ToolTipService.HasDropShadowProperty.AddOwner( 
                        typeof(ToolTip),
                        new FrameworkPropertyMetadata(null, 
                                                      new CoerceValueCallback(CoerceHasDropShadow)));

        private static object CoerceHasDropShadow(DependencyObject d, object value)
        { 
            ToolTip tt = (ToolTip)d;
 
            if (tt._parentPopup == null || !tt._parentPopup.AllowsTransparency || !SystemParameters.DropShadow) 
            {
                return BooleanBoxes.FalseBox; 
            }

            return PopupControlService.CoerceProperty(d, value, ToolTipService.HasDropShadowProperty);
        } 

        ///  
        ///     Whether the control has a drop shadow. 
        /// 
        public bool HasDropShadow 
        {
            get { return (bool)GetValue(HasDropShadowProperty); }
            set { SetValue(HasDropShadowProperty, value); }
        } 

        ///  
        /// The DependencyProperty for the PlacementTarget property 
        /// Default value: null
        ///  
        public static readonly DependencyProperty PlacementTargetProperty =
                    ToolTipService.PlacementTargetProperty.AddOwner(typeof(ToolTip),
                            new FrameworkPropertyMetadata(null,
                                                          new CoerceValueCallback(CoercePlacementTarget))); 

        private static object CoercePlacementTarget(DependencyObject d, object value) 
        { 
            return PopupControlService.CoerceProperty(d, value, ToolTipService.PlacementTargetProperty);
        } 


        /// 
        /// The UIElement relative to which this ToolTip will be displayed. 
        /// 
        [Bindable(true), Category("Layout")] 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] 
        public UIElement PlacementTarget
        { 
            get { return (UIElement) GetValue(PlacementTargetProperty); }
            set { SetValue(PlacementTargetProperty, value); }
        }
 
        /// 
        ///     The DependencyProperty for the PlacementRectangle property. 
        ///  
        public static readonly DependencyProperty PlacementRectangleProperty =
                    ToolTipService.PlacementRectangleProperty.AddOwner(typeof(ToolTip), 
                            new FrameworkPropertyMetadata(null,
                                                          new CoerceValueCallback(CoercePlacementRectangle)));

        private static object CoercePlacementRectangle(DependencyObject d, object value) 
        {
            return PopupControlService.CoerceProperty(d, value, ToolTipService.PlacementRectangleProperty); 
        } 

        ///  
        /// Get or set PlacementRectangle property of the ToolTip
        /// 
        [Bindable(true), Category("Layout")]
        public Rect PlacementRectangle 
        {
            get { return (Rect) GetValue(PlacementRectangleProperty); } 
            set { SetValue(PlacementRectangleProperty, value); } 
        }
 
        /// 
        /// The DependencyProperty for the Placement property
        /// Default value: null
        ///  
        public static readonly DependencyProperty PlacementProperty =
                    ToolTipService.PlacementProperty.AddOwner(typeof(ToolTip), 
                            new FrameworkPropertyMetadata(null, 
                                                          new CoerceValueCallback(CoercePlacement)));
 
        private static object CoercePlacement(DependencyObject d, object value)
        {
            return PopupControlService.CoerceProperty(d, value, ToolTipService.PlacementProperty);
        } 

        ///  
        ///     Chooses the behavior of where the Popup should be placed on screen. 
        /// 
        [Bindable(true), Category("Layout")] 
        public PlacementMode Placement
        {
            get { return (PlacementMode) GetValue(PlacementProperty); }
            set { SetValue(PlacementProperty, value); } 
        }
 
        ///  
        ///     The DependencyProperty for the CustomPopupPlacementCallback property.
        ///     Flags:              None 
        ///     Default Value:      null
        /// 
        public static readonly DependencyProperty CustomPopupPlacementCallbackProperty =
                    Popup.CustomPopupPlacementCallbackProperty.AddOwner(typeof(ToolTip)); 

        ///  
        ///     Chooses the behavior of where the Popup should be placed on screen. 
        /// 
        [Bindable(false), Category("Layout")] 
        public CustomPopupPlacementCallback CustomPopupPlacementCallback
        {
            get { return (CustomPopupPlacementCallback) GetValue(CustomPopupPlacementCallbackProperty); }
            set { SetValue(CustomPopupPlacementCallbackProperty, value); } 
        }
 
        ///  
        ///     The DependencyProperty for the StaysOpen property.
        ///     When false, the tool tip will close on the next mouse click 
        ///     Flags:              None
        ///     Default Value:      true
        /// 
        public static readonly DependencyProperty StaysOpenProperty = 
                    Popup.StaysOpenProperty.AddOwner(typeof(ToolTip));
 
        ///  
        ///     Chooses the behavior of when the Popup should automatically close.
        ///  
        [Bindable(true), Category("Behavior")]
        public bool StaysOpen
        {
            get { return (bool) GetValue(StaysOpenProperty); } 
            set { SetValue(StaysOpenProperty, value); }
        } 
 
        #endregion
 
        #region Public Events

        /// 
        ///     Opened event 
        /// 
        public static readonly RoutedEvent OpenedEvent = 
            EventManager.RegisterRoutedEvent("Opened", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ToolTip)); 

        ///  
        ///     Add/Remove event handler for Opened event
        /// 
        /// 
        public event RoutedEventHandler Opened 
        {
            add 
            { 
                AddHandler(OpenedEvent, value);
            } 
            remove
            {
                RemoveHandler(OpenedEvent, value);
            } 
        }
 
        ///  
        ///     Called when the Tooltip is opened. Also raises the OpenedEvent.
        ///  
        /// Generic routed event arguments.
        protected virtual void OnOpened(RoutedEventArgs e)
        {
            RaiseEvent(e); 
        }
 
        ///  
        ///     Closed event
        ///  
        public static readonly RoutedEvent ClosedEvent =
            EventManager.RegisterRoutedEvent("Closed", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ToolTip));

        ///  
        ///     Add/Remove event handler for Closed event
        ///  
        ///  
        public event RoutedEventHandler Closed
        { 
            add
            {
                AddHandler(ClosedEvent, value);
            } 
            remove
            { 
                RemoveHandler(ClosedEvent, value); 
            }
        } 

        /// 
        ///     Called when the ToolTip is closed. Also raises the ClosedEvent.
        ///  
        /// Generic routed event arguments.
        protected virtual void OnClosed(RoutedEventArgs e) 
        { 
            RaiseEvent(e);
        } 

        #endregion

        #region Protected Methods 

        ///  
        /// Creates AutomationPeer () 
        /// 
        protected override AutomationPeer OnCreateAutomationPeer() 
        {
            return new ToolTipAutomationPeer(this);
        }
 
        /// 
        /// Called when this element's visual parent changes 
        ///  
        /// 
        protected internal override void OnVisualParentChanged(DependencyObject oldParent) 
        {
            base.OnVisualParentChanged(oldParent);

            if (!Popup.IsRootedInPopup(_parentPopup, this)) 
            {
                throw new InvalidOperationException(SR.Get(SRID.ElementMustBeInPopup, "ToolTip")); 
            } 
        }
 
        internal override void OnAncestorChanged()
        {
            base.OnAncestorChanged();
 
            if (!Popup.IsRootedInPopup(_parentPopup, this))
            { 
                throw new InvalidOperationException(SR.Get(SRID.ElementMustBeInPopup, "ToolTip")); 
            }
        } 

        #endregion

        #region Private Methods 

        private void HookupParentPopup() 
        { 
            Debug.Assert(_parentPopup == null, "_parentPopup should be null");
 
            _parentPopup = new Popup();

            _parentPopup.AllowsTransparency = true;
 
            // When StaysOpen is true (default), make the popup window WS_EX_Transparent
            // to allow mouse input to go through the tooltip 
            _parentPopup.HitTestable = !StaysOpen; 

            // Coerce HasDropShadow property in case popup can't be transparent 
            CoerceValue(HasDropShadowProperty);

            // Listening to the Opened and Closed events lets us guarantee that
            // the popup is actually opened when we perform those functions. 
            _parentPopup.Opened += new EventHandler(OnPopupOpened);
            _parentPopup.Closed += new EventHandler(OnPopupClosed); 
            _parentPopup.PopupCouldClose += new EventHandler(OnPopupCouldClose); 

            _parentPopup.SetResourceReference(Popup.PopupAnimationProperty, SystemParameters.ToolTipPopupAnimationKey); 

            // Hooks up the popup properties from this menu to the popup so that
            // setting them on this control will also set them on the popup.
            Popup.CreateRootPopup(_parentPopup, this); 
        }
 
        internal void ForceClose() 
        {
            if (_parentPopup != null) 
            {
                _parentPopup.ForceClose();
            }
        } 

        private void OnPopupCouldClose(object sender, EventArgs e) 
        { 
            IsOpen = false;
        } 

        private void OnPopupOpened(object source, EventArgs e)
        {
            // Raise Accessibility event 
            if (AutomationPeer.ListenerExists(AutomationEvents.ToolTipOpened))
            { 
                AutomationPeer peer = UIElementAutomationPeer.CreatePeerForElement(this); 
                if (peer != null)
                { 
                    // We raise the event async to allow PopupRoot to hookup
                    Dispatcher.BeginInvoke(DispatcherPriority.Input, new DispatcherOperationCallback(delegate(object param)
                    {
                        peer.RaiseAutomationEvent(AutomationEvents.ToolTipOpened); 
                        return null;
                    }), null); 
                } 
            }
 
            OnOpened(new RoutedEventArgs(OpenedEvent, this));
        }

        private void OnPopupClosed(object source, EventArgs e) 
        {
            OnClosed(new RoutedEventArgs(ClosedEvent, this)); 
        } 

        #endregion 

        #region Data

        private Popup _parentPopup; 

        #endregion 
 
        #region DTypeThemeStyleKey
 
        // Returns the DependencyObjectType for the registered ThemeStyleKey's default
        // value. Controls will override this method to return approriate types.
        internal override DependencyObjectType DTypeThemeStyleKey
        { 
            get { return _dType; }
        } 
 
        private static DependencyObjectType _dType;
 
        #endregion DTypeThemeStyleKey
    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.
//---------------------------------------------------------------------------- 
//
// Copyright (C) Microsoft Corporation.  All rights reserved.
//
//--------------------------------------------------------------------------- 

using System; 
using System.Diagnostics; 
using System.ComponentModel;
 
using System.Collections;
using System.Collections.Specialized;
using System.Windows.Threading;
 
using System.Windows;
using System.Windows.Media; 
using System.Windows.Input; 
using System.Windows.Data;
using System.Windows.Automation.Peers; 
using System.Windows.Controls.Primitives;
using System.Windows.Markup;
using System.Windows.Shapes;
using MS.Utility; 
using MS.Internal.KnownBoxes;
 
namespace System.Windows.Controls 
{
    ///  
    /// A control to display information when the user hovers over a control
    /// 
    [DefaultEvent("Opened")]
    [Localizability(LocalizationCategory.ToolTip)] 
    public class ToolTip : ContentControl
    { 
        #region Constructors 

        static ToolTip() 
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof(ToolTip), new FrameworkPropertyMetadata(typeof(ToolTip)));
            _dType = DependencyObjectType.FromSystemTypeInternal(typeof(ToolTip));
            BackgroundProperty.OverrideMetadata(typeof(ToolTip), new FrameworkPropertyMetadata(SystemColors.InfoBrush)); 
            FocusableProperty.OverrideMetadata(typeof(ToolTip), new FrameworkPropertyMetadata(false));
        } 
 
        /// 
        /// Creates a default ToolTip 
        /// 
        public ToolTip() : base()
        {
        } 

        #endregion 
 
        #region Public Properties
 
        /// 
        /// The DependencyProperty for the HorizontalOffset property.
        /// Default: Length(0.0)
        ///  
        public static readonly DependencyProperty HorizontalOffsetProperty =
            ToolTipService.HorizontalOffsetProperty.AddOwner(typeof(ToolTip), 
                            new FrameworkPropertyMetadata(null, 
                                                          new CoerceValueCallback(CoerceHorizontalOffset)));
 
        private static object CoerceHorizontalOffset(DependencyObject d, object value)
        {
            return PopupControlService.CoerceProperty(d, value, ToolTipService.HorizontalOffsetProperty);
        } 

        ///  
        /// Horizontal offset from the default location when this ToolTIp is displayed 
        /// 
        [TypeConverter(typeof(LengthConverter))] 
        [Bindable(true), Category("Layout")]
        public double HorizontalOffset
        {
            get 
            {
                return (double)GetValue(HorizontalOffsetProperty); 
            } 
            set
            { 
                SetValue(HorizontalOffsetProperty, value);
            }
        }
 
        /// 
        /// The DependencyProperty for the VerticalOffset property. 
        /// Default: Length(0.0) 
        /// 
        public static readonly DependencyProperty VerticalOffsetProperty = 
            ToolTipService.VerticalOffsetProperty.AddOwner(typeof(ToolTip),
                            new FrameworkPropertyMetadata(null,
                                                          new CoerceValueCallback(CoerceVerticalOffset)));
 
        private static object CoerceVerticalOffset(DependencyObject d, object value)
        { 
            return PopupControlService.CoerceProperty(d, value, ToolTipService.VerticalOffsetProperty); 
        }
 
        /// 
        /// Vertical offset from the default location when this ToolTip is displayed
        /// 
        [TypeConverter(typeof(LengthConverter))] 
        [Bindable(true), Category("Layout")]
        public double VerticalOffset 
        { 
            get
            { 
                return (double)GetValue(VerticalOffsetProperty);
            }
            set
            { 
                SetValue(VerticalOffsetProperty, value);
            } 
        } 

        ///  
        /// DependencyProperty for the IsOpen property
        /// Default value: false
        /// 
        public static readonly DependencyProperty IsOpenProperty = 
                    DependencyProperty.Register(
                                "IsOpen", 
                                typeof(bool), 
                                typeof(ToolTip),
                                new FrameworkPropertyMetadata( 
                                            false,
                                            FrameworkPropertyMetadataOptions.BindsTwoWayByDefault,
                                            new PropertyChangedCallback(OnIsOpenChanged)));
 
        private static void OnIsOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        { 
            ToolTip t = (ToolTip) d; 

            if ((bool)e.NewValue) 
            {
                if (t._parentPopup == null)
                {
                    t.HookupParentPopup(); 
                }
            } 
            else 
            {
                // When ToolTip is about to close but still hooked up - we need to raise Accessibility event 
                if (AutomationPeer.ListenerExists(AutomationEvents.ToolTipClosed))
                {
                    AutomationPeer peer = UIElementAutomationPeer.CreatePeerForElement(t);
                    if (peer != null) 
                        peer.RaiseAutomationEvent(AutomationEvents.ToolTipClosed);
                } 
            } 
        }
 
        /// 
        /// Whether or not this ToolTip is visible
        /// 
        [Bindable(true), Browsable(false), Category("Appearance")] 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)]
        public bool IsOpen 
        { 
            get { return (bool) GetValue(IsOpenProperty); }
            set { SetValue(IsOpenProperty, value); } 
        }

        /// 
        ///     The DependencyProperty for HasDropShadow 
        /// 
        public static readonly DependencyProperty HasDropShadowProperty = 
                ToolTipService.HasDropShadowProperty.AddOwner( 
                        typeof(ToolTip),
                        new FrameworkPropertyMetadata(null, 
                                                      new CoerceValueCallback(CoerceHasDropShadow)));

        private static object CoerceHasDropShadow(DependencyObject d, object value)
        { 
            ToolTip tt = (ToolTip)d;
 
            if (tt._parentPopup == null || !tt._parentPopup.AllowsTransparency || !SystemParameters.DropShadow) 
            {
                return BooleanBoxes.FalseBox; 
            }

            return PopupControlService.CoerceProperty(d, value, ToolTipService.HasDropShadowProperty);
        } 

        ///  
        ///     Whether the control has a drop shadow. 
        /// 
        public bool HasDropShadow 
        {
            get { return (bool)GetValue(HasDropShadowProperty); }
            set { SetValue(HasDropShadowProperty, value); }
        } 

        ///  
        /// The DependencyProperty for the PlacementTarget property 
        /// Default value: null
        ///  
        public static readonly DependencyProperty PlacementTargetProperty =
                    ToolTipService.PlacementTargetProperty.AddOwner(typeof(ToolTip),
                            new FrameworkPropertyMetadata(null,
                                                          new CoerceValueCallback(CoercePlacementTarget))); 

        private static object CoercePlacementTarget(DependencyObject d, object value) 
        { 
            return PopupControlService.CoerceProperty(d, value, ToolTipService.PlacementTargetProperty);
        } 


        /// 
        /// The UIElement relative to which this ToolTip will be displayed. 
        /// 
        [Bindable(true), Category("Layout")] 
        [DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] 
        public UIElement PlacementTarget
        { 
            get { return (UIElement) GetValue(PlacementTargetProperty); }
            set { SetValue(PlacementTargetProperty, value); }
        }
 
        /// 
        ///     The DependencyProperty for the PlacementRectangle property. 
        ///  
        public static readonly DependencyProperty PlacementRectangleProperty =
                    ToolTipService.PlacementRectangleProperty.AddOwner(typeof(ToolTip), 
                            new FrameworkPropertyMetadata(null,
                                                          new CoerceValueCallback(CoercePlacementRectangle)));

        private static object CoercePlacementRectangle(DependencyObject d, object value) 
        {
            return PopupControlService.CoerceProperty(d, value, ToolTipService.PlacementRectangleProperty); 
        } 

        ///  
        /// Get or set PlacementRectangle property of the ToolTip
        /// 
        [Bindable(true), Category("Layout")]
        public Rect PlacementRectangle 
        {
            get { return (Rect) GetValue(PlacementRectangleProperty); } 
            set { SetValue(PlacementRectangleProperty, value); } 
        }
 
        /// 
        /// The DependencyProperty for the Placement property
        /// Default value: null
        ///  
        public static readonly DependencyProperty PlacementProperty =
                    ToolTipService.PlacementProperty.AddOwner(typeof(ToolTip), 
                            new FrameworkPropertyMetadata(null, 
                                                          new CoerceValueCallback(CoercePlacement)));
 
        private static object CoercePlacement(DependencyObject d, object value)
        {
            return PopupControlService.CoerceProperty(d, value, ToolTipService.PlacementProperty);
        } 

        ///  
        ///     Chooses the behavior of where the Popup should be placed on screen. 
        /// 
        [Bindable(true), Category("Layout")] 
        public PlacementMode Placement
        {
            get { return (PlacementMode) GetValue(PlacementProperty); }
            set { SetValue(PlacementProperty, value); } 
        }
 
        ///  
        ///     The DependencyProperty for the CustomPopupPlacementCallback property.
        ///     Flags:              None 
        ///     Default Value:      null
        /// 
        public static readonly DependencyProperty CustomPopupPlacementCallbackProperty =
                    Popup.CustomPopupPlacementCallbackProperty.AddOwner(typeof(ToolTip)); 

        ///  
        ///     Chooses the behavior of where the Popup should be placed on screen. 
        /// 
        [Bindable(false), Category("Layout")] 
        public CustomPopupPlacementCallback CustomPopupPlacementCallback
        {
            get { return (CustomPopupPlacementCallback) GetValue(CustomPopupPlacementCallbackProperty); }
            set { SetValue(CustomPopupPlacementCallbackProperty, value); } 
        }
 
        ///  
        ///     The DependencyProperty for the StaysOpen property.
        ///     When false, the tool tip will close on the next mouse click 
        ///     Flags:              None
        ///     Default Value:      true
        /// 
        public static readonly DependencyProperty StaysOpenProperty = 
                    Popup.StaysOpenProperty.AddOwner(typeof(ToolTip));
 
        ///  
        ///     Chooses the behavior of when the Popup should automatically close.
        ///  
        [Bindable(true), Category("Behavior")]
        public bool StaysOpen
        {
            get { return (bool) GetValue(StaysOpenProperty); } 
            set { SetValue(StaysOpenProperty, value); }
        } 
 
        #endregion
 
        #region Public Events

        /// 
        ///     Opened event 
        /// 
        public static readonly RoutedEvent OpenedEvent = 
            EventManager.RegisterRoutedEvent("Opened", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ToolTip)); 

        ///  
        ///     Add/Remove event handler for Opened event
        /// 
        /// 
        public event RoutedEventHandler Opened 
        {
            add 
            { 
                AddHandler(OpenedEvent, value);
            } 
            remove
            {
                RemoveHandler(OpenedEvent, value);
            } 
        }
 
        ///  
        ///     Called when the Tooltip is opened. Also raises the OpenedEvent.
        ///  
        /// Generic routed event arguments.
        protected virtual void OnOpened(RoutedEventArgs e)
        {
            RaiseEvent(e); 
        }
 
        ///  
        ///     Closed event
        ///  
        public static readonly RoutedEvent ClosedEvent =
            EventManager.RegisterRoutedEvent("Closed", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(ToolTip));

        ///  
        ///     Add/Remove event handler for Closed event
        ///  
        ///  
        public event RoutedEventHandler Closed
        { 
            add
            {
                AddHandler(ClosedEvent, value);
            } 
            remove
            { 
                RemoveHandler(ClosedEvent, value); 
            }
        } 

        /// 
        ///     Called when the ToolTip is closed. Also raises the ClosedEvent.
        ///  
        /// Generic routed event arguments.
        protected virtual void OnClosed(RoutedEventArgs e) 
        { 
            RaiseEvent(e);
        } 

        #endregion

        #region Protected Methods 

        ///  
        /// Creates AutomationPeer () 
        /// 
        protected override AutomationPeer OnCreateAutomationPeer() 
        {
            return new ToolTipAutomationPeer(this);
        }
 
        /// 
        /// Called when this element's visual parent changes 
        ///  
        /// 
        protected internal override void OnVisualParentChanged(DependencyObject oldParent) 
        {
            base.OnVisualParentChanged(oldParent);

            if (!Popup.IsRootedInPopup(_parentPopup, this)) 
            {
                throw new InvalidOperationException(SR.Get(SRID.ElementMustBeInPopup, "ToolTip")); 
            } 
        }
 
        internal override void OnAncestorChanged()
        {
            base.OnAncestorChanged();
 
            if (!Popup.IsRootedInPopup(_parentPopup, this))
            { 
                throw new InvalidOperationException(SR.Get(SRID.ElementMustBeInPopup, "ToolTip")); 
            }
        } 

        #endregion

        #region Private Methods 

        private void HookupParentPopup() 
        { 
            Debug.Assert(_parentPopup == null, "_parentPopup should be null");
 
            _parentPopup = new Popup();

            _parentPopup.AllowsTransparency = true;
 
            // When StaysOpen is true (default), make the popup window WS_EX_Transparent
            // to allow mouse input to go through the tooltip 
            _parentPopup.HitTestable = !StaysOpen; 

            // Coerce HasDropShadow property in case popup can't be transparent 
            CoerceValue(HasDropShadowProperty);

            // Listening to the Opened and Closed events lets us guarantee that
            // the popup is actually opened when we perform those functions. 
            _parentPopup.Opened += new EventHandler(OnPopupOpened);
            _parentPopup.Closed += new EventHandler(OnPopupClosed); 
            _parentPopup.PopupCouldClose += new EventHandler(OnPopupCouldClose); 

            _parentPopup.SetResourceReference(Popup.PopupAnimationProperty, SystemParameters.ToolTipPopupAnimationKey); 

            // Hooks up the popup properties from this menu to the popup so that
            // setting them on this control will also set them on the popup.
            Popup.CreateRootPopup(_parentPopup, this); 
        }
 
        internal void ForceClose() 
        {
            if (_parentPopup != null) 
            {
                _parentPopup.ForceClose();
            }
        } 

        private void OnPopupCouldClose(object sender, EventArgs e) 
        { 
            IsOpen = false;
        } 

        private void OnPopupOpened(object source, EventArgs e)
        {
            // Raise Accessibility event 
            if (AutomationPeer.ListenerExists(AutomationEvents.ToolTipOpened))
            { 
                AutomationPeer peer = UIElementAutomationPeer.CreatePeerForElement(this); 
                if (peer != null)
                { 
                    // We raise the event async to allow PopupRoot to hookup
                    Dispatcher.BeginInvoke(DispatcherPriority.Input, new DispatcherOperationCallback(delegate(object param)
                    {
                        peer.RaiseAutomationEvent(AutomationEvents.ToolTipOpened); 
                        return null;
                    }), null); 
                } 
            }
 
            OnOpened(new RoutedEventArgs(OpenedEvent, this));
        }

        private void OnPopupClosed(object source, EventArgs e) 
        {
            OnClosed(new RoutedEventArgs(ClosedEvent, this)); 
        } 

        #endregion 

        #region Data

        private Popup _parentPopup; 

        #endregion 
 
        #region DTypeThemeStyleKey
 
        // Returns the DependencyObjectType for the registered ThemeStyleKey's default
        // value. Controls will override this method to return approriate types.
        internal override DependencyObjectType DTypeThemeStyleKey
        { 
            get { return _dType; }
        } 
 
        private static DependencyObjectType _dType;
 
        #endregion DTypeThemeStyleKey
    }
}

// File provided for Reference Use Only by Microsoft Corporation (c) 2007.
// Copyright (c) Microsoft Corporation. All rights reserved.

                        

Link Menu

Network programming in C#, Network Programming in VB.NET, Network Programming in .NET
This book is available now!
Buy at Amazon US or
Buy at Amazon UK