MyRadioBox.xaml.vb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. Imports System.Windows.Markup
  2. Imports System.Windows.Shapes
  3. <ContentProperty("Inlines")>
  4. Public Class MyRadioBox
  5. Implements IMyRadio, ISettingControl
  6. '基础
  7. Public Uuid As Integer = GetUuid()
  8. Public Event PreviewCheck(sender As Object, e As RouteEventArgs)
  9. Public Event PreviewChange(sender As Object, e As RouteEventArgs)
  10. Public Event Check(sender As Object, e As RouteEventArgs) Implements IMyRadio.Check
  11. Public Event Changed(sender As Object, e As RouteEventArgs) Implements IMyRadio.Changed
  12. '自定义属性
  13. Public Property Checked As Boolean
  14. Get
  15. Return GetValue(CheckedProperty)
  16. End Get
  17. Set(value As Boolean)
  18. SetChecked(value, False)
  19. End Set
  20. End Property
  21. Public Shared ReadOnly CheckedProperty As DependencyProperty =
  22. DependencyProperty.Register("Checked", GetType(Boolean), GetType(MyRadioBox), New PropertyMetadata(False,
  23. Sub(d As MyRadioBox, e As DependencyPropertyChangedEventArgs)
  24. '在使用 XAML 设置 Checked 属性时,不会触发 Checked_Set 方法,所以需要在这里手动触发 UI 改变
  25. If Not d.IsLoaded Then d.SyncUI()
  26. End Sub))
  27. ''' <summary>
  28. ''' 手动设置 Checked 属性。
  29. ''' </summary>
  30. ''' <param name="value">新的 Checked 属性。</param>
  31. ''' <param name="user">是否由用户引发。</param>
  32. Public Sub SetChecked(value As Boolean, user As Boolean)
  33. Try
  34. 'Preview 事件
  35. If value AndAlso user Then
  36. Dim e = New RouteEventArgs(user)
  37. RaiseEvent PreviewCheck(Me, e)
  38. If e.Handled Then
  39. Radiobox_MouseLeave()
  40. Return
  41. End If
  42. End If
  43. '自定义属性基础
  44. Dim IsChanged As Boolean = False
  45. If IsLoaded AndAlso Not value = Checked Then RaiseEvent PreviewChange(Me, New RouteEventArgs(user))
  46. If value <> Checked Then
  47. SetValue(CheckedProperty, value)
  48. IsChanged = True
  49. End If
  50. '保证最多只有一个单选框选中
  51. If Parent Is Nothing Then Return
  52. Dim RadioboxList As New List(Of MyRadioBox)
  53. Dim CheckedCount As Integer = 0
  54. For Each Control In CType(Parent, Object).Children '收集控件列表与选中个数
  55. If TypeOf Control Is MyRadioBox Then
  56. RadioboxList.Add(Control)
  57. If Control.Checked Then CheckedCount += 1
  58. End If
  59. Next
  60. If CheckedCount > 1 Then
  61. If Checked Then
  62. '如果本控件选中,则取消其他所有控件的选中
  63. For Each Control As MyRadioBox In RadioboxList
  64. If Control.Checked AndAlso Not Control.Equals(Me) Then Control.Checked = False
  65. Next
  66. Else
  67. '如果本控件未选中,则只保留第一个选中的控件
  68. Dim FirstChecked = False
  69. For Each Control As MyRadioBox In RadioboxList
  70. If Control.Checked Then
  71. If FirstChecked Then
  72. Control.Checked = False '修改 Checked 会自动触发 Change 事件,所以不用额外触发
  73. Else
  74. FirstChecked = True
  75. End If
  76. End If
  77. Next
  78. End If
  79. End If
  80. If IsChanged Then
  81. '触发事件
  82. If Checked Then RaiseEvent Check(Me, New RouteEventArgs(user))
  83. RaiseEvent Changed(Me, New RouteEventArgs(user))
  84. RaiseCustomEvent()
  85. '更改动画
  86. SyncUI()
  87. End If
  88. Catch ex As Exception
  89. Logger.Error(ex, "单选框勾选改变错误", LogBehavior.Toast)
  90. End Try
  91. End Sub
  92. Private Sub SyncUI()
  93. Cursor = If(Checked, Nothing, Cursors.Hand)
  94. If AniControlEnabled = 0 AndAlso IsLoaded Then '防止默认属性变更触发动画
  95. If Checked Then
  96. '由无变有
  97. If ShapeDot.Opacity < 0.01 Then ShapeDot.Opacity = 1
  98. AniStart({
  99. AaScale(ShapeBorder, 10 - ShapeBorder.Width, AnimationTimeOfCheck, , New AniEaseOutFluent(AniEasePower.Weak), , True),
  100. AaScale(ShapeBorder, 8, AnimationTimeOfCheck * 2, AnimationTimeOfCheck * 0.6, New AniEaseOutBack, , True)
  101. }, "MyRadioBox Border " & Uuid)
  102. AniStart({
  103. AaScale(ShapeDot, 9 - ShapeDot.Width, AnimationTimeOfCheck * 2.6,, New AniEaseOutBack(AniEasePower.Weak), , True),
  104. AaOpacity(ShapeDot, 1 - ShapeDot.Opacity, AnimationTimeOfCheck * 0.5, AnimationTimeOfCheck * 0.6)
  105. }, "MyRadioBox Dot " & Uuid)
  106. AniStart({
  107. AaColor(ShapeBorder, Ellipse.StrokeProperty, If(IsMouseOver, "ColorBrush3", If(IsEnabled, "ColorBrush2", "ColorBrushGray5")), AnimationTimeOfCheck)
  108. }, "MyRadioBox BorderColor " & Uuid)
  109. Else
  110. '由有变无
  111. AniStart({
  112. AaScale(ShapeBorder, 18 - ShapeBorder.Width, AnimationTimeOfCheck, , New AniEaseOutFluent, , True)
  113. }, "MyRadioBox Border " & Uuid)
  114. AniStart({
  115. AaScale(ShapeDot, -ShapeDot.Width, AnimationTimeOfCheck, , New AniEaseInFluent, , True),
  116. AaOpacity(ShapeDot, -ShapeDot.Opacity, AnimationTimeOfCheck * 0.5, AnimationTimeOfCheck * 0.2)
  117. }, "MyRadioBox Dot " & Uuid)
  118. AniStart({
  119. AaColor(ShapeBorder, Ellipse.StrokeProperty, If(IsMouseOver, "ColorBrush3", If(IsEnabled, "ColorBrush1", "ColorBrushGray5")), AnimationTimeOfCheck)
  120. }, "MyRadioBox BorderColor " & Uuid)
  121. End If
  122. Else
  123. '不使用动画
  124. AniStop("MyRadioBox Border " & Uuid)
  125. AniStop("MyRadioBox Dot " & Uuid)
  126. AniStop("MyRadioBox BorderColor " & Uuid)
  127. If Checked Then
  128. ShapeDot.Width = 9
  129. ShapeDot.Height = 9
  130. ShapeDot.Opacity = 1
  131. ShapeDot.Margin = New Thickness(5.5, 0, 0, 0)
  132. ShapeBorder.SetResourceReference(Ellipse.StrokeProperty, If(IsEnabled, "ColorBrush2", "ColorBrushGray5"))
  133. Else
  134. ShapeDot.Width = 0
  135. ShapeDot.Height = 0
  136. ShapeDot.Opacity = 0
  137. ShapeDot.Margin = New Thickness(10, 0, 0, 0)
  138. ShapeBorder.SetResourceReference(Ellipse.StrokeProperty, If(IsEnabled, "ColorBrush1", "ColorBrushGray5"))
  139. End If
  140. End If
  141. End Sub
  142. Public ReadOnly Property Inlines As InlineCollection
  143. Get
  144. Return LabText.Inlines
  145. End Get
  146. End Property
  147. Public Property Text As String
  148. Get
  149. Return GetValue(TextProperty)
  150. End Get
  151. Set(value As String)
  152. SetValue(TextProperty, value)
  153. End Set
  154. End Property '内容
  155. Public Shared ReadOnly TextProperty As DependencyProperty = DependencyProperty.Register("Text", GetType(String), GetType(MyRadioBox), New PropertyMetadata(New PropertyChangedCallback(
  156. Sub(sender, e) If sender IsNot Nothing Then CType(sender, MyRadioBox).LabText.Text = e.NewValue)))
  157. '点击事件
  158. Private MouseDowned As Boolean = False
  159. Private AllowMouseDown As Boolean = True
  160. Private Sub Radiobox_MouseUp() Handles Me.MouseLeftButtonUp
  161. If Not MouseDowned Then Return
  162. Logger.Info($"按下单选框:{Text}")
  163. SetChecked(True, True)
  164. MouseDowned = False
  165. AniStart(AaColor(ShapeBorder, Ellipse.FillProperty, "ColorBrushHalfWhite", 100), "MyRadioBox Background " & Uuid)
  166. End Sub
  167. Private Sub Radiobox_MouseDown() Handles Me.MouseLeftButtonDown
  168. MouseDowned = True
  169. Focus()
  170. AniStart(AaColor(ShapeBorder, Ellipse.FillProperty, "ColorBrushBg1", 100), "MyRadioBox Background " & Uuid)
  171. If Not Checked Then
  172. AniStart(AaScale(ShapeBorder, 16.5 - ShapeBorder.Width, 1000, , New AniEaseOutFluent(AniEasePower.Strong), Absolute:=True), "MyRadioBox Border " & Uuid)
  173. End If
  174. End Sub
  175. Private Sub Radiobox_MouseLeave() Handles Me.MouseLeave
  176. If Not MouseDowned Then Return
  177. MouseDowned = False
  178. AniStart(AaColor(ShapeBorder, Ellipse.FillProperty, "ColorBrushHalfWhite", 100), "MyRadioBox Background " & Uuid)
  179. If Not Checked Then
  180. AniStart(AaScale(ShapeBorder, 18 - ShapeBorder.Width, 400, , New AniEaseOutFluent(AniEasePower.Strong), Absolute:=True), "MyRadioBox Border " & Uuid)
  181. End If
  182. End Sub
  183. '指向动画
  184. Private Const AnimationTimeOfMouseIn As Integer = 100 '鼠标指向动画长度
  185. Private Const AnimationTimeOfMouseOut As Integer = 200 '鼠标指向动画长度
  186. Private Const AnimationTimeOfCheck As Integer = 150 '勾选状态变更动画长度
  187. Private Sub MyRadioBox_Loaded() Handles Me.Loaded
  188. Cursor = If(Checked, Nothing, Cursors.Hand)
  189. End Sub
  190. Private Sub Radiobox_IsEnabledChanged() Handles Me.IsEnabledChanged
  191. If Me.IsLoaded AndAlso AniControlEnabled = 0 Then '防止默认属性变更触发动画
  192. '有动画
  193. If Me.IsEnabled Then
  194. '可用
  195. Radiobox_MouseLeaveAnimation()
  196. Else
  197. '不可用
  198. AniStart(AaColor(ShapeBorder, Ellipse.StrokeProperty, ColorGray5 - ShapeBorder.Stroke, AnimationTimeOfMouseOut), "MyRadioBox BorderColor " & Uuid)
  199. AniStart(AaColor(LabText, TextBlock.ForegroundProperty, ColorGray5 - LabText.Foreground, AnimationTimeOfMouseOut), "MyRadioBox TextColor " & Uuid)
  200. End If
  201. Else
  202. '无动画
  203. AniStop("MyRadioBox BorderColor " & Uuid)
  204. AniStop("MyRadioBox TextColor " & Uuid)
  205. LabText.SetResourceReference(TextBlock.ForegroundProperty, If(Me.IsEnabled, "ColorBrush1", "ColorBrushGray5"))
  206. ShapeBorder.SetResourceReference(Ellipse.StrokeProperty, If(Me.IsEnabled, If(Checked, "ColorBrush2", "ColorBrush1"), "ColorBrushGray5"))
  207. End If
  208. End Sub
  209. Private Sub Radiobox_MouseEnterAnimation() Handles Me.MouseEnter
  210. AniStart(AaColor(ShapeBorder, Ellipse.StrokeProperty, "ColorBrush3", AnimationTimeOfMouseIn), "MyRadioBox BorderColor " & Uuid)
  211. AniStart(AaColor(LabText, TextBlock.ForegroundProperty, "ColorBrush3", AnimationTimeOfMouseIn), "MyRadioBox TextColor " & Uuid)
  212. End Sub
  213. Private Sub Radiobox_MouseLeaveAnimation() Handles Me.MouseLeave
  214. If Not Me.IsEnabled Then Return 'MouseLeave 比 IsEnabledChanged 后执行,所以如果自定义事件修改了 IsEnabled,将导致显示错误
  215. If IsLoaded AndAlso AniControlEnabled = 0 Then
  216. AniStart(AaColor(ShapeBorder, Ellipse.StrokeProperty, If(IsEnabled, If(Checked, "ColorBrush2", "ColorBrush1"), "ColorBrushGray5"), AnimationTimeOfMouseOut), "MyRadioBox BorderColor " & Uuid)
  217. AniStart(AaColor(LabText, TextBlock.ForegroundProperty, If(IsEnabled, "ColorBrush1", "ColorBrushGray5"), AnimationTimeOfMouseOut), "MyRadioBox TextColor " & Uuid)
  218. Else
  219. AniStop("MyRadioBox BorderColor " & Uuid)
  220. AniStop("MyRadioBox TextColor " & Uuid)
  221. ShapeBorder.SetResourceReference(Ellipse.StrokeProperty, If(IsEnabled, If(Checked, "ColorBrush2", "ColorBrush1"), "ColorBrushGray5"))
  222. LabText.SetResourceReference(TextBlock.ForegroundProperty, If(IsEnabled, "ColorBrush1", "ColorBrushGray5"))
  223. End If
  224. End Sub
  225. #Region "设置"
  226. Private Sub RefreshSetting(NewValue As String) Implements ISettingControl.RefreshSetting
  227. Checked = NewValue = SettingService.GetValue(Me)
  228. End Sub
  229. Private Function GetCurrentSetting() As String Implements ISettingControl.GetCurrentSetting
  230. Return If(Checked, SettingService.GetValue(Me), Nothing)
  231. End Function
  232. Private Sub SaveSetting() Handles Me.Check
  233. SettingService.SaveSetting(Me)
  234. End Sub
  235. #End Region
  236. End Class