工具提示概述

工具提示是一个小弹出窗口,当用户将鼠标指针悬停在某个元素上(例如 Button)时显示。 本主题介绍工具提示,并讨论如何创建和自定义工具提示内容。

什么是工具提示

当用户将鼠标指针移到具有工具提示的元素上时,将显示一个包含工具提示内容的窗口(例如,描述控件函数的文本内容)在指定的时间内显示。 如果用户将鼠标指针移离控件,窗口将消失,因为工具提示内容无法接收焦点。

工具提示的内容可以包含一行或多行文本、图像、形状或其他视觉内容。 通过将以下属性之一设置为工具提示内容来定义控件的工具提示。

所使用的属性取决于定义工具提示的控件是否继承自 FrameworkContentElementFrameworkElement 类。

创建工具提示

以下示例演示如何通过将控件的属性Button设置为ToolTip文本字符串来创建简单的工具提示。

<Button ToolTip="Click to submit your information" 
        Click="SubmitCode" Height="20" Width="50">Submit</Button>

还可以将工具提示定义为 ToolTip 对象。 以下示例使用 XAML 将 ToolTip 对象指定为 TextBox 元素的工具提示。 请注意,示例通过设置FrameworkElement.ToolTip属性来指定ToolTip

<TextBox HorizontalAlignment="Left">ToolTip with non-text content
  <TextBox.ToolTip>
    <ToolTip>
      <DockPanel Width="50" Height="70">
        <Image Source="data\flower.jpg"/>
        <TextBlock>Useful information goes here.</TextBlock>
      </DockPanel>
    </ToolTip>
  </TextBox.ToolTip>
</TextBox>

以下示例使用代码生成 ToolTip 对象。 该示例创建一个ToolTiptt),并将其与一个Button关联。

button = new Button();
button.Content = "Hover over me.";
tt = new ToolTip();
tt.Content = "Created with C#";
button.ToolTip = tt;
cv2.Children.Add(button);
button = New Button()
button.Content = "Hover over me."
tt = New ToolTip()
tt.Content = "Created with Visual Basic"
button.ToolTip = tt
cv2.Children.Add(button)

还可以通过将工具提示内容括在布局元素(例如 DockPanel)中,来创建不定义为ToolTip对象的工具提示内容。 以下示例展示了如何将ToolTip的属性TextBox设置为由DockPanel控件包围的内容。

<TextBox>
  ToolTip with image and text
  <TextBox.ToolTip>
       <StackPanel>
        <Image Source="data\flower.jpg"/>
        <TextBlock>Useful information goes here.</TextBlock>
      </StackPanel>
  </TextBox.ToolTip>

使用 ToolTip 和 ToolTipService 类的属性

您可以通过设置视觉属性并应用样式来自定义工具提示的内容。 如果将工具提示内容定义为 ToolTip 对象,则可以设置对象的 ToolTip 视觉属性。 否则,必须在 ToolTipService 类上设置等效的附加属性。

有关如何设置属性以使用 ToolTipToolTipService 属性指定工具提示内容的位置的示例,请参阅 “定位工具提示”。

设置工具提示的样式

可以通过定义一个自定义的Style来设置ToolTip的样式。 下面的示例定义了一个Style,名为Simple,演示如何通过设定BackgroundForegroundFontSizeFontWeight来偏移ToolTip的位置并改变其外观。

<Style TargetType="ToolTip">
  <Setter Property = "HorizontalOffset" Value="10"/>
  <Setter Property = "VerticalOffset" Value="10"/>
  <Setter Property = "Background" Value="LightBlue"/>
  <Setter Property = "Foreground" Value="Purple"/>
  <Setter Property = "FontSize" Value="14"/>
  <Setter Property = "FontWeight" Value="Bold"/>
</Style>

使用 ToolTipService 的时间间隔属性

ToolTipService 类提供以下属性,用于设置工具提示显示时间: InitialShowDelayBetweenShowDelay以及 ShowDuration

使用InitialShowDelayShowDuration属性可以指定在ToolTip显示之前的延迟(通常很短),并指定ToolTip保持可见的时长。 有关详细信息,请参阅 “如何:延迟工具提示的显示”。

BetweenShowDelay 属性确定在不同控件之间快速移动鼠标指针时,工具提示是否不出现初始延迟。 有关该 BetweenShowDelay 属性的详细信息,请参阅 Use the BetweenShowDelay Property.

以下示例演示如何为工具提示设置这些属性。

<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>

另请参阅