如何:定位 ToolTip

此示例演示如何指定工具提示在屏幕上的位置。

示例

通过使用在 ToolTipToolTipService 类中定义的一组属性(包含五个属性),可以定位工具提示。 下表显示了这两组属性(每组中包含五个属性),并根据类提供了指向其参考文档的链接。

与类相对应的工具提示属性

System.Windows.Controls.ToolTip 类属性

System.Windows.Controls.ToolTipService 类属性

ToolTip.Placement

ToolTipService.Placement

ToolTip.PlacementTarget

ToolTipService.PlacementTarget

ToolTip.PlacementRectangle

ToolTipService.PlacementRectangle

ToolTip.HorizontalOffset

ToolTipService.HorizontalOffset

ToolTip.VerticalOffset

ToolTipService.VerticalOffset

如果使用 ToolTip 对象定义工具提示的内容,则可以使用任一类的属性;但是,ToolTipService 属性优先级较高。 对未定义为 ToolTip 对象的工具提示使用 ToolTipService 属性。

下图演示如何使用这些属性定位工具提示。 虽然,这些图中的 Extensible Application Markup Language (XAML) 示例演示了如何设置由 ToolTip 类定义的属性,但是 ToolTipService 类的对应属性仍然遵循相同的布局规则。 有关可能的 Placement 属性值的更多信息,请参见Popup 放置行为

使用 Placement 属性定位工具提示

ToolTip 定位

使用 Placement 和 PlacementRectangle 属性定位工具提示

通过使用定位矩形来定位 ToolTip

使用 Placement、PlacementRectangle 和 Offset 属性定位工具提示

ToolTip 定位示意图

下面的示例演示如何使用 ToolTip 属性指定内容为 ToolTip 对象的工具提示的位置。

      <Ellipse Height="25" Width="50" 
               Fill="Gray" 
               HorizontalAlignment="Left"
               ToolTipService.InitialShowDelay="1000"
               ToolTipService.ShowDuration="7000"
               ToolTipService.BetweenShowDelay="2000">
        <Ellipse.ToolTip>
          <ToolTip Placement="Right" 
                   PlacementRectangle="50,0,0,0"
                   HorizontalOffset="10" 
                   VerticalOffset="20"
                   HasDropShadow="false"
                   Opened="whenToolTipOpens"
                   Closed="whenToolTipCloses"
                   >
            <BulletDecorator>
              <BulletDecorator.Bullet>
                <Ellipse Height="10" Width="20" Fill="Blue"/>
              </BulletDecorator.Bullet>
              <TextBlock>Uses the ToolTip Class</TextBlock>
            </BulletDecorator>
          </ToolTip>
        </Ellipse.ToolTip>
      </Ellipse>

            'Create an ellipse that will have a 
            'ToolTip control. 
            Dim ellipse1 As New Ellipse()
            ellipse1.Height = 25
            ellipse1.Width = 50
            ellipse1.Fill = Brushes.Gray
            ellipse1.HorizontalAlignment = HorizontalAlignment.Left

            'Create a tooltip and set its position.
            Dim tooltip As New ToolTip()
            tooltip.Placement = PlacementMode.Right
            tooltip.PlacementRectangle = New Rect(50, 0, 0, 0)
            tooltip.HorizontalOffset = 10
            tooltip.VerticalOffset = 20

            'Create BulletDecorator and set it
            'as the tooltip content.
            Dim bdec As New BulletDecorator()
            Dim littleEllipse As New Ellipse()
            littleEllipse.Height = 10
            littleEllipse.Width = 20
            littleEllipse.Fill = Brushes.Blue
            bdec.Bullet = littleEllipse
            Dim tipText As New TextBlock()
            tipText.Text = "Uses the ToolTip class"
            bdec.Child = tipText
            tooltip.Content = bdec

            'set tooltip on ellipse
            ellipse1.ToolTip = tooltip
//Create an ellipse that will have a 
//ToolTip control. 
Ellipse ellipse1 = new Ellipse();
ellipse1.Height = 25;
ellipse1.Width = 50;
ellipse1.Fill = Brushes.Gray;
ellipse1.HorizontalAlignment = HorizontalAlignment.Left;

//Create a tooltip and set its position.
ToolTip tooltip = new ToolTip();
tooltip.Placement = PlacementMode.Right;
tooltip.PlacementRectangle = new Rect(50, 0, 0, 0);
tooltip.HorizontalOffset = 10;
tooltip.VerticalOffset = 20;

//Create BulletDecorator and set it
//as the tooltip content.
BulletDecorator bdec = new BulletDecorator();
Ellipse littleEllipse = new Ellipse();
littleEllipse.Height = 10;
littleEllipse.Width = 20;
littleEllipse.Fill = Brushes.Blue;
bdec.Bullet = littleEllipse;
TextBlock tipText = new TextBlock();
tipText.Text = "Uses the ToolTip class";
bdec.Child = tipText;
tooltip.Content = bdec;

//set tooltip on ellipse
ellipse1.ToolTip = tooltip;

下面的示例演示如何使用 ToolTipService 属性指定内容不是 ToolTip 对象的工具提示的位置。

<Ellipse Height="25" Width="50" 
      Fill="Gray" 
      HorizontalAlignment="Left"
      ToolTipService.InitialShowDelay="1000"
      ToolTipService.ShowDuration="7000"
      ToolTipService.BetweenShowDelay="2000"
      ToolTipService.Placement="Right" 
      ToolTipService.PlacementRectangle="50,0,0,0"
      ToolTipService.HorizontalOffset="10" 
      ToolTipService.VerticalOffset="20"
      ToolTipService.HasDropShadow="false"
      ToolTipService.ShowOnDisabled="true" 
      ToolTipService.IsEnabled="true"
      ToolTipOpening="whenToolTipOpens"
      ToolTipClosing="whenToolTipCloses"
      >
  <Ellipse.ToolTip>
    <BulletDecorator>
      <BulletDecorator.Bullet>
        <Ellipse Height="10" Width="20" Fill="Blue"/>
      </BulletDecorator.Bullet>
      <TextBlock>Uses the ToolTipService class</TextBlock>
    </BulletDecorator>
  </Ellipse.ToolTip>
</Ellipse>

            'Create and Ellipse with the BulletDecorator as 
            'the tooltip 
            Dim ellipse2 As New Ellipse()
            ellipse2.Name = "ellipse2"
            Me.RegisterName(ellipse2.Name, ellipse2)
            ellipse2.Height = 25
            ellipse2.Width = 50
            ellipse2.Fill = Brushes.Gray
            ellipse2.HorizontalAlignment = HorizontalAlignment.Left

            'set tooltip timing
            ToolTipService.SetInitialShowDelay(ellipse2, 1000)
            ToolTipService.SetBetweenShowDelay(ellipse2, 2000)
            ToolTipService.SetShowDuration(ellipse2, 7000)

            'set tooltip placement

            ToolTipService.SetPlacement(ellipse2, PlacementMode.Right)

            ToolTipService.SetPlacementRectangle(ellipse2, New Rect(50, 0, 0, 0))

            ToolTipService.SetHorizontalOffset(ellipse2, 10.0)

            ToolTipService.SetVerticalOffset(ellipse2, 20.0)


            ToolTipService.SetHasDropShadow(ellipse2, False)

            ToolTipService.SetIsEnabled(ellipse2, True)

            ToolTipService.SetShowOnDisabled(ellipse2, True)

            ellipse2.AddHandler(ToolTipService.ToolTipOpeningEvent, New RoutedEventHandler(AddressOf whenToolTipOpens))
            ellipse2.AddHandler(ToolTipService.ToolTipClosingEvent, New RoutedEventHandler(AddressOf whenToolTipCloses))

            'define tooltip content
            Dim bdec2 As New BulletDecorator()
            Dim littleEllipse2 As New Ellipse()
            littleEllipse2.Height = 10
            littleEllipse2.Width = 20
            littleEllipse2.Fill = Brushes.Blue
            bdec2.Bullet = littleEllipse2
            Dim tipText2 As New TextBlock()
            tipText2.Text = "Uses the ToolTipService class"
            bdec2.Child = tipText2
            ToolTipService.SetToolTip(ellipse2, bdec2)
            stackPanel_1_2.Children.Add(ellipse2)

            //Create and Ellipse with the BulletDecorator as 
            //the tooltip 
            Ellipse ellipse2 = new Ellipse();
            ellipse2.Name = "ellipse2";
            this.RegisterName(ellipse2.Name, ellipse2);
            ellipse2.Height = 25;
            ellipse2.Width = 50;
            ellipse2.Fill = Brushes.Gray;
            ellipse2.HorizontalAlignment = HorizontalAlignment.Left;

            //set tooltip timing
            ToolTipService.SetInitialShowDelay(ellipse2, 1000);
            ToolTipService.SetBetweenShowDelay(ellipse2, 2000);
            ToolTipService.SetShowDuration(ellipse2, 7000);

            //set tooltip placement

            ToolTipService.SetPlacement(ellipse2, PlacementMode.Right);

            ToolTipService.SetPlacementRectangle(ellipse2,
                new Rect(50, 0, 0, 0));

            ToolTipService.SetHorizontalOffset(ellipse2, 10.0);

            ToolTipService.SetVerticalOffset(ellipse2, 20.0);


            ToolTipService.SetHasDropShadow(ellipse2, false);

            ToolTipService.SetIsEnabled(ellipse2, true);

            ToolTipService.SetShowOnDisabled(ellipse2, true);

            ellipse2.AddHandler(ToolTipService.ToolTipOpeningEvent,
                new RoutedEventHandler(whenToolTipOpens));
            ellipse2.AddHandler(ToolTipService.ToolTipClosingEvent,
                new RoutedEventHandler(whenToolTipCloses));

            //define tooltip content
            BulletDecorator bdec2 = new BulletDecorator();
            Ellipse littleEllipse2 = new Ellipse();
            littleEllipse2.Height = 10;
            littleEllipse2.Width = 20;
            littleEllipse2.Fill = Brushes.Blue;
            bdec2.Bullet = littleEllipse2;
            TextBlock tipText2 = new TextBlock();
            tipText2.Text = "Uses the ToolTipService class";
            bdec2.Child = tipText2;
            ToolTipService.SetToolTip(ellipse2, bdec2);
            stackPanel_1_2.Children.Add(ellipse2);

请参见

任务

如何:在已禁用的控件上启用 ContextMenu

参考

ToolTip

ToolTipService

概念

ToolTip 概述

其他资源

ToolTip 帮助主题