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
- MenuItemBinding.cs
- RuntimeArgumentHandle.cs
- MetadataPropertyAttribute.cs
- Point.cs
- HttpConfigurationSystem.cs
- FlowPosition.cs
- InputReport.cs
- CodeEventReferenceExpression.cs
- ExplicitDiscriminatorMap.cs
- AuthenticationManager.cs
- GridViewPageEventArgs.cs
- ToolStripPanelRenderEventArgs.cs
- Tokenizer.cs
- ApplyHostConfigurationBehavior.cs
- TextureBrush.cs
- ListBoxItemAutomationPeer.cs
- DataContractSerializerOperationGenerator.cs
- CqlBlock.cs
- PointAnimationUsingKeyFrames.cs
- QueryProcessor.cs
- _HTTPDateParse.cs
- StringInfo.cs
- TableRowCollection.cs
- AddInControllerImpl.cs
- SafeBuffer.cs
- ListSourceHelper.cs
- MaxSessionCountExceededException.cs
- ProcessInfo.cs
- EmissiveMaterial.cs
- Globals.cs
- DataAccessor.cs
- XmlMemberMapping.cs
- FunctionImportMapping.cs
- GlobalEventManager.cs
- AllowedAudienceUriElement.cs
- ChangeBlockUndoRecord.cs
- List.cs
- Int32CollectionConverter.cs
- DbConnectionHelper.cs
- MatcherBuilder.cs
- DecoderExceptionFallback.cs
- ProcessModelInfo.cs
- ProcessProtocolHandler.cs
- X509Extension.cs
- UIElementAutomationPeer.cs
- MethodCallConverter.cs
- BuildManager.cs
- DefaultDialogButtons.cs
- WebBrowserEvent.cs
- InvokeMethod.cs
- BaseParser.cs
- ChtmlFormAdapter.cs
- State.cs
- ConnectionManagementElement.cs
- PageSetupDialog.cs
- ChameleonKey.cs
- DatatypeImplementation.cs
- MachineKeyConverter.cs
- LinkDescriptor.cs
- StyleXamlParser.cs
- FloaterBaseParaClient.cs
- StandardBindingCollectionElement.cs
- SqlPersonalizationProvider.cs
- BitmapEditor.cs
- Int32Collection.cs
- DelegatingMessage.cs
- Mouse.cs
- AccessDataSourceWizardForm.cs
- InternalCompensate.cs
- ProtocolsConfigurationEntry.cs
- SelectionEditor.cs
- Condition.cs
- Command.cs
- AccessDataSource.cs
- Int32AnimationBase.cs
- ToolStripLocationCancelEventArgs.cs
- XsltException.cs
- AnonymousIdentificationSection.cs
- EventRoute.cs
- PopupEventArgs.cs
- ResourceReader.cs
- ByteRangeDownloader.cs
- SqlDataSourceConfigureSelectPanel.cs
- CalendarDay.cs
- TimerEventSubscriptionCollection.cs
- HtmlToClrEventProxy.cs
- EntityDataSourceStatementEditor.cs
- ScrollProviderWrapper.cs
- Baml2006ReaderSettings.cs
- CustomUserNameSecurityTokenAuthenticator.cs
- InkCollectionBehavior.cs
- EncoderReplacementFallback.cs
- SerializerWriterEventHandlers.cs
- TextBoxView.cs
- FocusWithinProperty.cs
- StateRuntime.cs
- AutomationPropertyInfo.cs
- Button.cs
- SinglePhaseEnlistment.cs
- FormViewModeEventArgs.cs