赞
踩
表示一个控件,该控件可创建一个弹出窗口,以便在界面中显示元素的信息。
ToolTip是 ContentControl ,这意味着它可以包含任何类型的单个对象 (例如字符串、图像或面板) 。 有关更多信息,请参见 ContentControl 类。
使用 ToolTip 控件为用户提供信息。 例如,可以在Button 或ToolBarTray的 ToolBar中使用ToolTip 显示提示信息 。 ToolTip控件的内容可以从简单的文本字符串到更复杂的内容(例如具有嵌入式文本和图像的StackPanel)变化。ToolTip无法获得焦点。
ToolTip控件不能有父级。 例如,不能 Button 将的 Content 属性设置为 ToolTip ,而是将 ToolTip 分配给 FrameworkContentElement.ToolTip 和 FrameworkElement.ToolTip 属性。
通过在ToolTip中定义,可以在多个 ToolTip 元素上使用ResourceDictionary 。
ToolTip类的属性用于定义ToolTip的位置和行为。其中的许多属性也是在类中定义的 ToolTipService 。 如果其中一个属性的值由 ToolTip 属性和 ToolTipService 附加属性指定,则 ToolTipService 使用属性的值。 此外, ToolTipService 类还提供属性来设置计时行为。 这些附加属性不包含在类中, ToolTip 但可由控件使用。
自定义 ToolTip 控件
若要对多个 ToolTip 控件应用相同的属性设置,请使用 Style 属性。 您可以修改 ControlTemplate 默认值,为控件指定独特的外观。 有关创建 ControlTemplate 的详细信息,请参阅 通过创建 System.windows.controls.controltemplate> 自定义现有控件的外观。 若要查看特定于 ToolTip 的部分和状态,请参阅 工具提示样式和模板。
此控件的依赖属性可能由控件的默认样式设置。 如果按默认样式设置属性,则当控件出现在应用程序中时,属性可能会更改为默认值。 默认样式取决于应用程序运行时使用的桌面主题。 有关详细信息,请参阅 默认的 WPF 主题。
只有视觉对象属性已存在于控件的默认模板中并且已使用 TemplateBinding 设置时,设置该属性才有效。 在通过创建 ControlTemplate 自定义现有控件的外观一文的更改控件的视觉结构部分可以找到视觉属性列表。
名称 | 备注 |
ClosedEvent | 标识 Closed 路由事件。 |
CustomPopupPlacementCallbackProperty | 标识 CustomPopupPlacementCallback 依赖项属性。 |
HasDropShadowProperty | 标识 HasDropShadow 依赖项属性。 |
HorizontalOffsetProperty | 标识 HorizontalOffset 依赖项属性。 |
IsOpenProperty | 标识 IsOpen 依赖项属性。 |
OpenedEvent | 标识 Opened 路由事件。 |
PlacementProperty | 标识 Placement 依赖项属性。 |
PlacementRectangleProperty | 标识 PlacementRectangle 依赖项属性。 |
PlacementTargetProperty | 标识 PlacementTarget 依赖项属性。 |
StaysOpenProperty | 标识 StaysOpen 依赖项属性。 |
VerticalOffsetProperty | 标识 VerticalOffset 依赖项属性。 |
名称 | 备注 | 权限 |
CustomPopupPlacementCallback | 获取或设置用于放置此 ToolTip 的委托处理程序方法。 | get; set; |
---|---|---|
HasDropShadow | 获取或设置一个指示控件是否有投影的值。 | get; set; |
HorizontalOffset | 获取或设置目标原点与弹出项对齐点之间的水平距离。 | get; set; |
IsOpen | 获取或设置一个表示 ToolTip 是否可见的值。 | get; set; |
Placement | 获取或设置 ToolTip 控件打开时的方向,并指定 ToolTip 控件在与屏幕边界重叠时的行为。 | get; set; |
PlacementRectangle | 获取或设置 ToolTip 控件打开时所在位置与之相关的矩形区域。 | get; set; |
PlacementTarget | 获取或设置 UIElement,当它打开时相对于它确定 ToolTip 的位置。 | get; set; |
StaysOpen | 获取或设置一个值,该值指示打开的 ToolTip 是否一直保持打开状态,直到用户在鼠标不在 ToolTip 上时单击鼠标。 | get; set; |
VerticalOffset | 获取或设置目标原点与弹出项对齐点之间的垂直距离。 | get; set; |
名称 | 备注 | 权限 |
响应 Closed 事件 | protected | |
当 Content 属性更改时调用。 | protected | |
创建 AutomationPeer 控件的 ToolTip 实现。 | protected | |
响应 Opened 事件。 | protected | |
响应 ToolTip 的可视父级的更改。 | protected |
表示一项服务,该服务提供用于控制ToolTip的显示和行为的属性和事件。
您可以通过设置FrameworkContentElement.ToolTip或FrameworkElement.ToolTip属性来为元素定义工具提示。ToolTip属性可以设置一个子元素。子项的内容可以从简单的文本字符串到更复杂的内容(例如具有嵌入式文本和Image元素、StackPanel)不等。
您可以将ToolTip content 定义为ToolTip对象,但这不是必需的。当您未将工具提示内容定义为ToolTip对象时,可以使用ToolTipService属性来定位和自定义工具提示内容。ToolTipService类的附加属性用于确定工具提示的位置,行为和外观。这些属性在定义工具提示的元素上设置。
ToolTip类和ToolTipService类共享许多用于自定义工具提示的相同属性。如果同时设置了等效的ToolTip和ToolTipService属性,则ToolTipService属性具有优先权。例如,如果同时为System.Windows.Controls.ToolTip对象设置了ToolTip.Placement属性和ToolTipService.Placement属性,则将使用ToolTipService.Placement属性的值。
以下 ToolTipService 时间相关的属性仅为ToolTipService类定义,并由所有tooltip使用:
有关如何处理工具提示打开或关闭时发生的事件的信息,请参见ToolTipOpeningEvent和ToolTipClosingEvent字段。
Placement,PlacementRectangle,PlacementTarget,HorizontalOffset和VerticalOffset的行为类似于Popup类中同名的属性。有关更多信息,请参见弹出窗口放置行为。
名称 | 备注 | 权限 |
---|---|---|
标识 BetweenShowDelay 附加属性。 | public static readonly | |
标识 HasDropShadow 附加属性。 | public static readonly | |
标识 HorizontalOffset 附加属性。 | public static readonly | |
标识 InitialShowDelay 附加属性。 | public static readonly | |
标识 IsEnabled 附加属性。 | public static readonly | |
标识 IsOpen 附加属性。 | public static readonly | |
标识 Placement 附加属性。 | public static readonly | |
标识 PlacementRectangle 附加属性。 | public static readonly | |
标识 PlacementTarget 附加属性。 | public static readonly | |
标识 ShowDuration 附加属性。 | public static readonly | |
标识 ShowOnDisabled 附加属性。 | public static readonly | |
标识由使用 ToolTipClosing 服务显示工具提示的对象公开的 ToolTipService 事件。 | public static readonly | |
标识由使用 ToolTipOpening 服务显示工具提示的对象公开的 ToolTipService 事件。 | public static readonly | |
标识 ToolTip 附加属性。 | public static readonly | |
标识 VerticalOffset 附加属性。 | public static readonly |
名称 | 备注 | 权限 |
---|---|---|
AddToolTipClosingHandler | 为 ToolTipClosing 附加事件添加处理程序。 | public |
AddToolTipOpeningHandler | 为 ToolTipOpening 附加事件添加处理程序。 | public |
GetBetweenShowDelay | 获取对象的 BetweenShowDelay 附加属性的值。 | public |
GetHasDropShadow | 获取对象的 HasDropShadow 附加属性的值。 | public |
GetHorizontalOffset | 获取对象的 HorizontalOffset 附加属性的值。 | public |
GetInitialShowDelay | 获取对象的 InitialShowDelay 附加属性的值。 | public |
GetIsEnabled | 获取对象的 IsEnabled 附加属性的值。 | public |
GetIsOpen | 获取对象的 IsOpen 附加属性的值。 | public |
GetPlacement | 获取对象的 Placement 附加属性的值。 | public |
GetPlacementRectangle | 获取对象的 PlacementRectangle 附加属性的值。 | public |
GetPlacementTarget | 获取对象的 PlacementTarget 附加属性的值。 | public |
GetShowDuration | 获取对象的 ShowDuration 附加属性的值。 | public |
GetShowOnDisabled | 获取对象的 ShowOnDisabled 附加属性的值。 | public |
GetToolTip | 获取对象的 ToolTip 附加属性的值。 | public |
GetVerticalOffset | 获取对象的 VerticalOffset 附加属性的值。 | public |
RemoveToolTipClosingHandler | 移除 ToolTipClosing 附加事件的处理程序。 | public |
RemoveToolTipOpeningHandler | 移除 ToolTipOpening 附加事件的处理程序。 | public |
SetBetweenShowDelay | 设置对象的 BetweenShowDelay 附加属性的值。 | public |
SetHasDropShadow | 设置对象的 HasDropShadow 附加属性的值。 | public |
SetHorizontalOffset | 设置对象的 HorizontalOffset 附加属性的值。 | public |
SetInitialShowDelay | 设置对象的 InitialShowDelay 附加属性的值。 | public |
SetIsEnabled | 设置对象的 IsEnabled 附加属性的值。 | public |
SetPlacement | 设置对象的 Placement 附加属性的值。 | public |
SetPlacementRectangle | 设置对象的 PlacementRectangle 附加属性的值。 | public |
SetPlacementTarget | 设置对象的 PlacementTarget 附加属性的值。 | public |
SetShowDuration | 设置对象的 ShowDuration 附加属性的值。 | public |
SetShowOnDisabled | 设置对象的 ShowOnDisabled 附加属性的值。 | public |
SetToolTip | 设置对象的 ToolTip 附加属性的值。 | public |
SetVerticalOffset | 设置对象的 VerticalOffset 附加属性的值。 | public |
名称 | 备注 | 对应方法 |
---|---|---|
BetweenShowDelay | 获取或设置当第二个工具提示在没有延迟的情况下显示时,两个工具提示的显示之间的最大时间。 | GetBetweenShowDelay、SetBetweenShowDelay |
HasDropShadow | 获取或设置工具提示是否显示投影效果。 | GetHasDropShadow、SetHasDropShadow |
HorizontalOffset | 获取或设置由 PlacementRectangle 和 PlacementTarget 属性为工具提示指定的区域的左侧偏移量。 | GetHorizontalOffset、SetHorizontalOffset |
InitialShowDelay | 获取或设置工具提示打开之前的时间长度。 | GetInitialShowDelay、SetInitialShowDelay |
IsEnabled | 获取或设置是否显示工具提示。 | GetIsEnabled、SetIsEnabled |
IsOpen | 获取工具提示当前是否可见 | GetIsOpen |
Placement | 获取或设置工具提示在打开时的方向,并指定工具提示在与屏幕边界重叠时的行为方式。 | GetPlacement、SetPlacement |
PlacementRectangle | 获取或设置相对于其来放置工具提示的矩形区域。 | GetPlacementRectangle、SetPlacementRectangle |
PlacementTarget | 获取或设置相对于其来放置工具提示的对象。 | GetPlacementTarget、SetPlacementTarget |
ShowDuration | 获取或设置工具提示保持可见的时间。 | GetShowDuration、SetShowDuration |
ShowOnDisabled | 获取或设置是否为未启用的对象显示工具提示。 | GetShowOnDisabled、SetShowOnDisabled |
ToolTip | 获取或设置工具提示的内容。 | GetToolTip、SetToolTip |
VerticalOffset | 获取或设置由 PlacementRectangle 和 PlacementTarget 属性为工具提示指定的区域的顶部距离。 | GetVerticalOffset、SetVerticalOffset |
- <Window
- x:Class="ToolTip.MainWindow"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- xmlns:local="clr-namespace:ToolTip"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- Title="MainWindow"
- Width="400"
- Height="400"
- mc:Ignorable="d" x:Name="win1">
- <Grid x:Name="grid1">
- <StackPanel>
- <TextBox Height="30"/>
- <TextBox x:Name="tbx1" Height="30">
- <TextBox.ToolTip>
- <ToolTip HasDropShadow="True"
- HorizontalOffset="100"
- IsOpen="False"
- Placement="Right"
- PlacementRectangle="0,0,0,0"
- StaysOpen="True"
- VerticalOffset="0"
- Opened="OnOpened"
- Closed="OnClosed" >
- <StackPanel>
- <Label Background="LightBlue" Content="This is a ToolTip!" />
- <Button Background="LightSalmon" Content="This Button Can't Press." />
- </StackPanel>
- </ToolTip>
- </TextBox.ToolTip>
- </TextBox>
- <TextBox x:Name="tbx2" Height="30"/>
- </StackPanel>
- </Grid>
- </Window>
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
-
- namespace ToolTip
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
- }
-
- private void OnOpened(object sender, RoutedEventArgs e)
- {
- foreach (var item in (((sender as System.Windows.Controls.ToolTip).PlacementTarget as FrameworkElement).Parent as Panel).Children)
- {
- if (item is TextBox)
- if ((item as TextBox) == tbx2) (item as TextBox).Text = "Opened";
- }
- }
-
- private void OnClosed(object sender, RoutedEventArgs e)
- {
- foreach (var item in (((sender as System.Windows.Controls.ToolTip).PlacementTarget as FrameworkElement).Parent as Panel).Children)
- {
- if (item is TextBox)
- if ((item as TextBox) == tbx2) (item as TextBox).Text = "Closed";
- }
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- using System.Windows.Controls;
- using System.Windows.Data;
- using System.Windows.Documents;
- using System.Windows.Input;
- using System.Windows.Media;
- using System.Windows.Media.Imaging;
- using System.Windows.Navigation;
- using System.Windows.Shapes;
-
-
- namespace ToolTip
- {
- /// <summary>
- /// MainWindow.xaml 的交互逻辑
- /// </summary>
- public partial class MainWindow : Window
- {
- public MainWindow()
- {
- InitializeComponent();
-
-
- Grid grid = (this as Window).Content as Grid;
-
- StackPanel stackPanel = new StackPanel();
-
- TextBox textBox = new TextBox();
- TextBox tbx1 = new TextBox();
- TextBox tbx2 = new TextBox();
-
- textBox.Height = 30;
- tbx1.Height = 30;
- tbx2.Height = 30; tbx2.Name = "tbx2";
-
- System.Windows.Controls.ToolTip toolTip = new System.Windows.Controls.ToolTip();
- toolTip.HasDropShadow = true;
- toolTip.HorizontalOffset = 100;
- toolTip.VerticalAlignment = 0;
- toolTip.PlacementRectangle = new Rect(0, 0, 0, 0);
- toolTip.IsOpen = false;
- toolTip.Placement = System.Windows.Controls.Primitives.PlacementMode.Right;
- toolTip.StaysOpen = true;
- toolTip.Opened += OnOpened;
- toolTip.Closed += OnClosed;
-
- StackPanel stackPanel1 = new StackPanel();
- Label label = new Label();
- Button button = new Button();
-
- label.Background = Brushes.LightBlue;
- label.Content = "This is a ToolTip!";
-
- button.Background = Brushes.LightSalmon;
- button.Content = "This Button Can't Press.";
-
- stackPanel1.Children.Add(label);
- stackPanel1.Children.Add(button);
-
- toolTip.Content = stackPanel1;
-
- tbx1.ToolTip = toolTip;
-
- stackPanel.Children.Add(textBox);
- stackPanel.Children.Add(tbx1);
- stackPanel.Children.Add(tbx2);
-
- grid.Children.Add(stackPanel);
- }
-
-
-
-
- private void OnOpened(object sender, RoutedEventArgs e)
- {
- foreach (var item in (((sender as System.Windows.Controls.ToolTip).PlacementTarget as FrameworkElement).Parent as Panel).Children)
- {
- if (item is TextBox)
- if ((item as TextBox).Name == "tbx2") (item as TextBox).Text = "Opened";
- }
- }
-
- private void OnClosed(object sender, RoutedEventArgs e)
- {
- foreach (var item in (((sender as System.Windows.Controls.ToolTip).PlacementTarget as FrameworkElement).Parent as Panel).Children)
- {
- if (item is TextBox)
- if ((item as TextBox).Name == "tbx2") (item as TextBox).Text = "Closed";
- }
- }
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。