Silverlight实用窍门系列:13.基于Popup浮动窗体控件模拟ToolTip的实现【附带实例源码】

本节是基于第四节的源码基础上进行扩展的源码,故本节源码附带有鼠标右键和全屏功能。

  1. 在本节,我们将讲述一个小技巧:使用Popup浮动窗体控件模拟Tip悬浮效果。此技巧的原理是当鼠标移动到某个控件上面的时候会触发MouseMove事件,此时设置Popup控件显示出来。触发此事件时能够获知此时的鼠标坐标位置,我们根据鼠标位置的变化的同时移动Popup控件的HorizontalOffset属性(获取或设置目标原点与弹出项对齐点之间的水平距离)和VerticalOffset(获取或设置目标原点与弹出项对齐点之间的垂直距离)。如果鼠标移出自定义控件,那么设置Popup浮动窗体的IsOpen属性为false隐藏此浮动窗体。
  2. 创建一个自定义控件命名为TipWindow,其XAML代码如下:

ExpandedBlockStart.gif

复制代码

  1. <
  2. UserControl x:Class
  3. =
  4. "
  5. SLRightMouseButton.TipWindow
  6. "
  7. xmlns
  8. =
  9. "
  10. http://schemas.microsoft.com/winfx/2006/xaml/presentation
  11. "
  12. xmlns:x
  13. =
  14. "
  15. http://schemas.microsoft.com/winfx/2006/xaml
  16. "
  17. xmlns:d
  18. =
  19. "
  20. http://schemas.microsoft.com/expression/blend/2008
  21. "
  22. xmlns:mc
  23. =
  24. "
  25. http://schemas.openxmlformats.org/markup-compatibility/2006
  26. "
  27. mc:Ignorable
  28. =
  29. "
  30. d
  31. "
  32. d:DesignHeight
  33. =
  34. "
  35. 300
  36. "
  37. d:DesignWidth
  38. =
  39. "
  40. 400
  41. "
  42. xmlns:sdk
  43. =
  44. "
  45. http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk
  46. "
  47. >
  48. <
  49. Grid x:Name
  50. =
  51. "
  52. LayoutRoot
  53. "
  54. Background
  55. =
  56. "
  57. White
  58. "
  59. >
  60. <
  61. Popup x:Name
  62. =
  63. "
  64. tip
  65. "
  66. >
  67. <
  68. Grid x:Name
  69. =
  70. "
  71. toolTip
  72. "
  73. Width
  74. =
  75. "
  76. 312
  77. "
  78. Height
  79. =
  80. "
  81. 200
  82. "
  83. >
  84. <
  85. Grid.Background
  86. >
  87. <
  88. ImageBrush ImageSource
  89. =
  90. "
  91. ./BG.png
  92. "
  93. ></
  94. ImageBrush
  95. >
  96. </
  97. Grid.Background
  98. >
  99. <
  100. Grid.RowDefinitions
  101. >
  102. <
  103. RowDefinition Height
  104. =
  105. "
  106. auto
  107. "
  108. />
  109. <
  110. RowDefinition
  111. />
  112. </
  113. Grid.RowDefinitions
  114. >
  115. <
  116. Grid.ColumnDefinitions
  117. >
  118. <
  119. ColumnDefinition Width
  120. =
  121. "
  122. auto
  123. "
  124. />
  125. </
  126. Grid.ColumnDefinitions
  127. >
  128. <
  129. TextBlock Grid.Row
  130. =
  131. "
  132. 0
  133. "
  134. Grid.Column
  135. =
  136. "
  137. 0
  138. "
  139. x:Name
  140. =
  141. "
  142. mainContent
  143. "
  144. Foreground
  145. =
  146. "
  147. White
  148. "
  149. FontSize
  150. =
  151. "
  152. 14
  153. "
  154. Height
  155. =
  156. "
  157. Auto
  158. "
  159. HorizontalAlignment
  160. =
  161. "
  162. Center
  163. "
  164. Margin
  165. =
  166. "
  167. 0 20 0 0
  168. "
  169. VerticalAlignment
  170. =
  171. "
  172. Top
  173. "
  174. />
  175. </
  176. Grid
  177. >
  178. </
  179. Popup
  180. >
  181. </
  182. Grid
  183. >
  184. </
  185. UserControl
  186. >

复制代码

  1. <Grid.Background>
  2. <ImageBrush ImageSource="./BG.png"></ImageBrush>
  3. </Grid.Background>
  4. 这几句话设置整个Grid的背景图片为当前目录下的BG.png,所以我们也要在项目中引入该文件。再来看看TipWindow.cs的关键源代码如下:

复制代码

  1. public
  2. void
  3. BindControl(UIElement uc) { uc.MouseLeave
  4. +=
  5. new
  6. MouseEventHandler(ti_MouseLeave); uc.MouseMove
  7. +=
  8. new
  9. MouseEventHandler(ti_MouseMove); }
  10. public
  11. void
  12. CancelBindControl(UIElement uc) { uc.MouseLeave
  13. -=
  14. new
  15. MouseEventHandler(ti_MouseLeave); uc.MouseMove
  16. -=
  17. new
  18. MouseEventHandler(ti_MouseMove); }
  19. ///
  20. <summary>
  21. ///
  22. 鼠标离开自定义控件时,隐藏提示框
  23. ///
  24. </summary>
  25. ///
  26. <param name="sender"></param>
  27. ///
  28. <param name="e"></param>
  29. private
  30. void
  31. ti_MouseLeave(
  32. object
  33. sender, MouseEventArgs e) {
  34. this
  35. .tip.IsOpen
  36. =
  37. false
  38. ; }
  39. ///
  40. <summary>
  41. ///
  42. 鼠标在自定义控件上移动式时,提示框也跟随移动
  43. ///
  44. </summary>
  45. ///
  46. <param name="sender"></param>
  47. ///
  48. <param name="e"></param>
  49. private
  50. void
  51. ti_MouseMove(
  52. object
  53. sender, MouseEventArgs e) {
  54. this
  55. .tip.IsOpen
  56. =
  57. true
  58. ;
  59. this
  60. .tip.HorizontalOffset
  61. =
  62. e.GetPosition(
  63. null
  64. ).X
  65. -
  66. 158
  67. ;
  68. this
  69. .tip.VerticalOffset
  70. =
  71. e.GetPosition(
  72. null
  73. ).Y
  74. -
  75. 202
  76. ; }

复制代码

  1. 此关键代码中有一个绑定函数BindControl(UIElement uc),让需要获得TipWindow浮动框的uc控件绑定MouseMoveMouseLeave事件。现在再创建一个新的自定义控件Rect.xaml,并且添加一个IsFlag属性,此属性指示Rect自定义控件是否显示TipWindow浮动框。XAML代码里面添加一个矩形控件ti,代码如下:
  2. <
  3. Rectangle x:Name
  4. =
  5. "
  6. ti
  7. "
  8. Width
  9. =
  10. "
  11. 50
  12. "
  13. Height
  14. =
  15. "
  16. 50
  17. "
  18. RadiusX
  19. =
  20. "
  21. 5
  22. "
  23. RadiusY
  24. =
  25. "
  26. 5
  27. "
  28. Fill
  29. =
  30. "
  31. YellowGreen
  32. "
  33. ></
  34. Rectangle
  35. >
  36. Rect.xaml.cs代码中,我们输入以下代码即可:

复制代码

  1. private
  2. bool
  3. _IsFlag; TipWindow tipWindow
  4. =
  5. new
  6. TipWindow();
  7. ///
  8. <summary>
  9. ///
  10. 是否显示TipWindow
  11. ///
  12. </summary>
  13. public
  14. bool
  15. IsFlag {
  16. get
  17. {
  18. return
  19. _IsFlag; }
  20. set
  21. { _IsFlag
  22. =
  23. value;
  24. if
  25. (_IsFlag
  26. ==
  27. true
  28. ) { tipWindow.BindControl(
  29. this
  30. .ti); }
  31. else
  32. {
  33. //
  34. tipWindow.CancelBindControl(this.ti);
  35. } } }

复制代码

  1. Rect自定义控件制作好了,在MainPage.xaml.cs文件中,实例化此自定义控件,并且设置其IsFlag属性为True即可看到下面图片的效果了。

MainPage.xaml.cs:

  1. Rect rect
  2. =
  3. new
  4. Rect(); rect.Margin
  5. =
  6. new
  7. Thickness(
  8. 250
  9. ); rect.IsFlag
  10. =
  11. true
  12. ;
  13. this
  14. .LayoutRoot.Children.Add(rect);

2011022516550981.jpg

  1. 本实例采用VS2010+Silverlight 4.0编写,如需源码请点击 [SLTipWindow.rar][] 下载

发表评论

表情:
评论列表 (有 0 条评论,30人围观)

还没有评论,来说两句吧...

相关阅读