Code:
/ DotNET / DotNET / 8.0 / untmp / WIN_WINDOWS / lh_tools_devdiv_wpf / Windows / wcp / 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.
Link Menu
This book is available now!
Buy at Amazon US or
Buy at Amazon UK
- GridPatternIdentifiers.cs
- Rect3DConverter.cs
- CodeTypeDelegate.cs
- XmlText.cs
- DetailsViewDeleteEventArgs.cs
- PaperSource.cs
- SamlSerializer.cs
- TheQuery.cs
- RelatedCurrencyManager.cs
- XsdBuildProvider.cs
- SettingsPropertyNotFoundException.cs
- ExpressionEditorAttribute.cs
- SqlConnectionFactory.cs
- PointCollection.cs
- SubMenuStyleCollectionEditor.cs
- SafeArrayTypeMismatchException.cs
- GPPOINTF.cs
- ScriptingWebServicesSectionGroup.cs
- ObjectListSelectEventArgs.cs
- TextUtf8RawTextWriter.cs
- BamlResourceSerializer.cs
- ProtocolsConfigurationEntry.cs
- CommentAction.cs
- DataViewManager.cs
- ToolStripProgressBar.cs
- HttpApplication.cs
- SymbolType.cs
- XmlArrayItemAttribute.cs
- ViewKeyConstraint.cs
- XmlBindingWorker.cs
- XPathNode.cs
- XmlObjectSerializerContext.cs
- WindowsIPAddress.cs
- ExpressionList.cs
- CipherData.cs
- Publisher.cs
- IdentityHolder.cs
- DecoderFallback.cs
- ProgressBarAutomationPeer.cs
- DataTableTypeConverter.cs
- CapabilitiesAssignment.cs
- Vector3DAnimationBase.cs
- TemplateParser.cs
- _BasicClient.cs
- ServiceObjectContainer.cs
- Native.cs
- HMAC.cs
- ChangeTracker.cs
- HttpCookie.cs
- FormViewDesigner.cs
- SQLDecimal.cs
- XmlSchemaSubstitutionGroup.cs
- StaticTextPointer.cs
- RepeaterDataBoundAdapter.cs
- SmiGettersStream.cs
- UnitySerializationHolder.cs
- ThrowHelper.cs
- HashHelper.cs
- DesignerOptions.cs
- ListViewContainer.cs
- SmiContextFactory.cs
- ActivityCollectionMarkupSerializer.cs
- ServiceHttpHandlerFactory.cs
- printdlgexmarshaler.cs
- _IPv6Address.cs
- ObjectToken.cs
- Normalization.cs
- IntegrationExceptionEventArgs.cs
- Point3DAnimation.cs
- OperationGenerator.cs
- unitconverter.cs
- ComboBoxItem.cs
- NativeMethods.cs
- DateTime.cs
- CommandEventArgs.cs
- DataGridItemAttachedStorage.cs
- SerializationException.cs
- FormsAuthenticationTicket.cs
- XXXOnTypeBuilderInstantiation.cs
- prompt.cs
- XPathQilFactory.cs
- DockAndAnchorLayout.cs
- BufferedGraphics.cs
- TextBoxRenderer.cs
- DeploymentExceptionMapper.cs
- ConsumerConnectionPointCollection.cs
- BrowserInteropHelper.cs
- Rijndael.cs
- CommonXSendMessage.cs
- HttpSocketManager.cs
- TextEditorCopyPaste.cs
- SqlCommandSet.cs
- InputLangChangeRequestEvent.cs
- SerialPinChanges.cs
- SafeViewOfFileHandle.cs
- SpeakCompletedEventArgs.cs
- SelectionRangeConverter.cs
- WindowsStatusBar.cs
- DataGridColumnHeadersPresenterAutomationPeer.cs
- PersonalizationAdministration.cs