翼度科技»论坛 编程开发 .net 查看内容

浅谈WPF之ToolTip工具提示

3

主题

3

帖子

9

积分

新手上路

Rank: 1

积分
9
在日常应用中,当鼠标放置在某些控件上时,都会有相应的信息提示,从软件易用性上来说,这是一个非常友好的功能设计。那在WPF中,如何进行控件信息提示呢?这就是本文需要介绍的ToolTip【工具提示】内容,本文以一些简单的小例子,简述如何在WPF开发中,应用工具提示,仅供学习分享使用,如有不足之处,还请指正。

 
什么是工具提示?

 
工具提示是一个小型的弹出窗口,在用户将鼠标指针悬停在某个元素(如 Button)上时显示。当用户将鼠标指针移动到具有工具提示的元素上时,将在一段指定的时间内显示一个包含工具提示内容(例如,介绍控件功能的文本内容)的窗口。 如果用户将鼠标指针从控件上移开,该窗口将消失,因为工具提示内容无法接收焦点。工具提示的内容可以包含一行或多行文本、图像、形状或其他可视内容。 
创建工具提示

 
凡是继承自FrameworkElement和FrameworkContentElement的控件,都具有ToolTip属性,为object类型,即接收任何类型的属性设置。
 
1. 本文工具提示

 
可以将一段文本赋值给控件的ToolTip属性,如下所示:
  1. [/code]文本工具提示截图
  2. [align=center][/align]
  3.  
  4. [size=5][b]2. 自定义工具提示[/b][/size]
  5.  
  6. [b]工具提示可以是一段文本,也可以是一个组合的自定义对象[/b]。比如:一个带有图标的工具提示,如下所示 :
  7. [code]<Button Content='工具提示' Width="120" Height="30" Grid.Column="1">
  8. <Window.Resources>
  9.    
  10. </Window.Resources><Button.ToolTip>
  11. <Window.Resources>
  12.    
  13. </Window.Resources><Window.Resources>
  14.    
  15. </Window.Resources><StackPanel Orientation="Horizontal">
  16. <Window.Resources>
  17.    
  18. </Window.Resources><Window.Resources>
  19.    
  20. </Window.Resources><Window.Resources>
  21.    
  22. </Window.Resources><Path Data="{StaticResource icon_info}" Stroke="#F69661" Stretch="Fill" StrokeThickness="1" Fill="#F69661" Width="20" Height="20" VerticalAlignment="Center" Margin="2"></Path>
  23. <Window.Resources>
  24.    
  25. </Window.Resources><Window.Resources>
  26.    
  27. </Window.Resources><Window.Resources>
  28.    
  29. </Window.Resources><TextBlock Text="这是一个带图标的工具提示" VerticalAlignment="Center" Margin="2"></TextBlock>
  30. <Window.Resources>
  31.    
  32. </Window.Resources><Window.Resources>
  33.    
  34. </Window.Resources></StackPanel>
  35. <Window.Resources>
  36.    
  37. </Window.Resources></Button.ToolTip>
  38. </Button>
复制代码
以上示例,工具提示为一个图标,一个文本提示,水平排列。以为ToolTip的内容只能有一个Child,所以用StackPanel包括起来,作为一个整体。
自定义工具提示截图

 
设置工具提示样式

 
工具提示和普通的控件一样,也可以设置样式【如:背景色,前景色,字体大小等】,如下所示:
  1. <Window.Resources>
  2.    
  3. </Window.Resources>
复制代码
设置样式后的工具提示,如下图所示:

 
定位工具提示

 
可以使用在 ToolTip 和 ToolTipService 类中定义的五个属性集来定位工具提示。 下表显示这两组五个属性,并根据类提供指向其参考文档的链接。

使用Placement定位
Placement的值是一个枚举,常用的有:Bottom,Top,Left,Right,Center等。下图显示使用 Placement 属性放置工具提示:

下图显示使用 Placement 和 PlacementRectangle 属性放置工具提示:

下图显示使用 Placement、PlacementRectangle 和 Offset 属性放置工具提示:

工具提示定位综合示例,如下所示:
  1. <Button Content='工具提示' Width="120" Height="30" Grid.Column="1">
  2. <Window.Resources>
  3.    
  4. </Window.Resources><Button.ToolTip>
  5. <Window.Resources>
  6.    
  7. </Window.Resources><Window.Resources>
  8.    
  9. </Window.Resources><StackPanel Orientation="Horizontal">
  10. <Window.Resources>
  11.    
  12. </Window.Resources><Window.Resources>
  13.    
  14. </Window.Resources><Window.Resources>
  15.    
  16. </Window.Resources><Path Data="{StaticResource icon_info}" Stroke="#F69661" Stretch="Fill" StrokeThickness="1" Fill="#F69661" Width="20" Height="20" VerticalAlignment="Center" Margin="2"></Path>
  17. <Window.Resources>
  18.    
  19. </Window.Resources><Window.Resources>
  20.    
  21. </Window.Resources><Window.Resources>
  22.    
  23. </Window.Resources><TextBlock Text="这是一个带图标的工具提示" VerticalAlignment="Center" Margin="2"></TextBlock>
  24. <Window.Resources>
  25.    
  26. </Window.Resources><Window.Resources>
  27.    
  28. </Window.Resources></StackPanel>
  29. <Window.Resources>
  30.    
  31. </Window.Resources></Button.ToolTip>
  32. </Button><Window.Resources>
  33.    
  34. </Window.Resources><Window.Resources>
  35.    
  36. </Window.Resources><Window.Resources>
  37.    
  38. </Window.Resources><Window.Resources>
  39.    
  40. </Window.Resources><Window.Resources>
  41.    
  42. </Window.Resources><Window.Resources>
  43.    
  44. </Window.Resources><Window.Resources>
  45.    
  46. </Window.Resources><Window.Resources>
  47.    
  48. </Window.Resources><Window.Resources>
  49.    
  50. </Window.Resources><Window.Resources>
  51.    
  52. </Window.Resources><Window.Resources>
  53.    
  54. </Window.Resources>Uses the ToolTip Class<Window.Resources>
  55.    
  56. </Window.Resources><Window.Resources>
  57.    
  58. </Window.Resources><Window.Resources>
  59.    
  60. </Window.Resources><Window.Resources>
  61.    
  62. </Window.Resources><Window.Resources>
  63.    
  64. </Window.Resources><Window.Resources>
  65.    
  66. </Window.Resources>
复制代码
示例截图如下所示:

 

参考文献

 
1. 官方文档:https://learn.microsoft.com/zh-cn/dotnet/desktop/wpf/controls/tooltip-overview?view=netframeworkdesktop-4.8
 
以上就是【浅谈WPF之ToolTip工具提示】的全部内容,希望能够一起学习,共同进步。

来源:https://www.cnblogs.com/hsiang/archive/2023/12/27/17927272.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
来自手机

举报 回复 使用道具