MyRadioBox.xaml.vb 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 Not 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. Select Case CheckedCount '判断选中情况
  61. Case 0
  62. '没有任何单选框被选中,选择第一个
  63. RadioboxList(0).Checked = True
  64. Case Is > 1
  65. '选中项目多于 1 个
  66. If Checked Then
  67. '如果本控件选中,则取消其他所有控件的选中
  68. For Each Control As MyRadioBox In RadioboxList
  69. If Control.Checked AndAlso Not Control.Equals(Me) Then Control.Checked = False
  70. Next
  71. Else
  72. '如果本控件未选中,则只保留第一个选中的控件
  73. Dim FirstChecked = False
  74. For Each Control As MyRadioBox In RadioboxList
  75. If Control.Checked Then
  76. If FirstChecked Then
  77. Control.Checked = False '修改 Checked 会自动触发 Change 事件,所以不用额外触发
  78. Else
  79. FirstChecked = True
  80. End If
  81. End If
  82. Next
  83. End If
  84. End Select
  85. If IsChanged Then
  86. '触发事件
  87. If Checked Then RaiseEvent Check(Me, New RouteEventArgs(user))
  88. RaiseEvent Changed(Me, New RouteEventArgs(user))
  89. RaiseCustomEvent()
  90. '更改动画
  91. SyncUI()
  92. End If
  93. Catch ex As Exception
  94. Log(ex, "单选框勾选改变错误", NotifyLevel.AllUsers)
  95. End Try
  96. End Sub
  97. Private Sub SyncUI()
  98. If AniControlEnabled = 0 AndAlso IsLoaded Then '防止默认属性变更触发动画
  99. If Checked Then
  100. '由无变有
  101. If ShapeDot.Opacity < 0.01 Then ShapeDot.Opacity = 1
  102. AniStart({
  103. AaScale(ShapeBorder, 10 - ShapeBorder.Width, AnimationTimeOfCheck, , New AniEaseOutFluent(AniEasePower.Weak), , True),
  104. AaScale(ShapeBorder, 8, AnimationTimeOfCheck * 2, AnimationTimeOfCheck * 0.6, New AniEaseOutBack, , True)
  105. }, "MyRadioBox Border " & Uuid)
  106. AniStart({
  107. AaScale(ShapeDot, 9 - ShapeDot.Width, AnimationTimeOfCheck * 2.6,, New AniEaseOutBack(AniEasePower.Weak), , True),
  108. AaOpacity(ShapeDot, 1 - ShapeDot.Opacity, AnimationTimeOfCheck * 0.5, AnimationTimeOfCheck * 0.6)
  109. }, "MyRadioBox Dot " & Uuid)
  110. AniStart({
  111. AaColor(ShapeBorder, Ellipse.StrokeProperty, If(IsMouseOver, "ColorBrush3", If(IsEnabled, "ColorBrush2", "ColorBrushGray5")), AnimationTimeOfCheck)
  112. }, "MyRadioBox BorderColor " & Uuid)
  113. Else
  114. '由有变无
  115. AniStart({
  116. AaScale(ShapeBorder, 18 - ShapeBorder.Width, AnimationTimeOfCheck, , New AniEaseOutFluent, , True)
  117. }, "MyRadioBox Border " & Uuid)
  118. AniStart({
  119. AaScale(ShapeDot, -ShapeDot.Width, AnimationTimeOfCheck, , New AniEaseInFluent, , True),
  120. AaOpacity(ShapeDot, -ShapeDot.Opacity, AnimationTimeOfCheck * 0.5, AnimationTimeOfCheck * 0.2)
  121. }, "MyRadioBox Dot " & Uuid)
  122. AniStart({
  123. AaColor(ShapeBorder, Ellipse.StrokeProperty, If(IsMouseOver, "ColorBrush3", If(IsEnabled, "ColorBrush1", "ColorBrushGray5")), AnimationTimeOfCheck)
  124. }, "MyRadioBox BorderColor " & Uuid)
  125. End If
  126. Else
  127. '不使用动画
  128. AniStop("MyRadioBox Border " & Uuid)
  129. AniStop("MyRadioBox Dot " & Uuid)
  130. AniStop("MyRadioBox BorderColor " & Uuid)
  131. If Checked Then
  132. ShapeDot.Width = 9
  133. ShapeDot.Height = 9
  134. ShapeDot.Opacity = 1
  135. ShapeDot.Margin = New Thickness(5.5, 0, 0, 0)
  136. ShapeBorder.SetResourceReference(Ellipse.StrokeProperty, If(IsEnabled, "ColorBrush2", "ColorBrushGray5"))
  137. Else
  138. ShapeDot.Width = 0
  139. ShapeDot.Height = 0
  140. ShapeDot.Opacity = 0
  141. ShapeDot.Margin = New Thickness(10, 0, 0, 0)
  142. ShapeBorder.SetResourceReference(Ellipse.StrokeProperty, If(IsEnabled, "ColorBrush1", "ColorBrushGray5"))
  143. End If
  144. End If
  145. End Sub
  146. Public ReadOnly Property Inlines As InlineCollection
  147. Get
  148. Return LabText.Inlines
  149. End Get
  150. End Property
  151. Public Property Text As String
  152. Get
  153. Return GetValue(TextProperty)
  154. End Get
  155. Set(value As String)
  156. SetValue(TextProperty, value)
  157. End Set
  158. End Property '内容
  159. Public Shared ReadOnly TextProperty As DependencyProperty = DependencyProperty.Register("Text", GetType(String), GetType(MyRadioBox), New PropertyMetadata(New PropertyChangedCallback(
  160. Sub(sender, e) If sender IsNot Nothing Then CType(sender, MyRadioBox).LabText.Text = e.NewValue)))
  161. '点击事件
  162. Private MouseDowned As Boolean = False
  163. Private AllowMouseDown As Boolean = True
  164. Private Sub Radiobox_MouseUp() Handles Me.MouseLeftButtonUp
  165. If Not MouseDowned Then Return
  166. Log("[Control] 按下单选框:" & Text)
  167. SetChecked(True, True)
  168. MouseDowned = False
  169. AniStart(AaColor(ShapeBorder, Ellipse.FillProperty, "ColorBrushHalfWhite", 100), "MyRadioBox Background " & Uuid)
  170. End Sub
  171. Private Sub Radiobox_MouseDown() Handles Me.MouseLeftButtonDown
  172. MouseDowned = True
  173. Focus()
  174. AniStart(AaColor(ShapeBorder, Ellipse.FillProperty, "ColorBrushBg1", 100), "MyRadioBox Background " & Uuid)
  175. If Not Checked Then
  176. AniStart(AaScale(ShapeBorder, 16.5 - ShapeBorder.Width, 1000, , New AniEaseOutFluent(AniEasePower.Strong), Absolute:=True), "MyRadioBox Border " & Uuid)
  177. End If
  178. End Sub
  179. Private Sub Radiobox_MouseLeave() Handles Me.MouseLeave
  180. If Not MouseDowned Then Return
  181. MouseDowned = False
  182. AniStart(AaColor(ShapeBorder, Ellipse.FillProperty, "ColorBrushHalfWhite", 100), "MyRadioBox Background " & Uuid)
  183. If Not Checked Then
  184. AniStart(AaScale(ShapeBorder, 18 - ShapeBorder.Width, 400, , New AniEaseOutFluent(AniEasePower.Strong), Absolute:=True), "MyRadioBox Border " & Uuid)
  185. End If
  186. End Sub
  187. '指向动画
  188. Private Const AnimationTimeOfMouseIn As Integer = 100 '鼠标指向动画长度
  189. Private Const AnimationTimeOfMouseOut As Integer = 200 '鼠标指向动画长度
  190. Private Const AnimationTimeOfCheck As Integer = 150 '勾选状态变更动画长度
  191. Private Sub Radiobox_IsEnabledChanged() Handles Me.IsEnabledChanged
  192. If Me.IsLoaded AndAlso AniControlEnabled = 0 Then '防止默认属性变更触发动画
  193. '有动画
  194. If Me.IsEnabled Then
  195. '可用
  196. Radiobox_MouseLeaveAnimation()
  197. Else
  198. '不可用
  199. AniStart(AaColor(ShapeBorder, Ellipse.StrokeProperty, ColorGray5 - ShapeBorder.Stroke, AnimationTimeOfMouseOut), "MyRadioBox BorderColor " & Uuid)
  200. AniStart(AaColor(LabText, TextBlock.ForegroundProperty, ColorGray5 - LabText.Foreground, AnimationTimeOfMouseOut), "MyRadioBox TextColor " & Uuid)
  201. End If
  202. Else
  203. '无动画
  204. AniStop("MyRadioBox BorderColor " & Uuid)
  205. AniStop("MyRadioBox TextColor " & Uuid)
  206. LabText.SetResourceReference(TextBlock.ForegroundProperty, If(Me.IsEnabled, "ColorBrush1", "ColorBrushGray5"))
  207. ShapeBorder.SetResourceReference(Ellipse.StrokeProperty, If(Me.IsEnabled, If(Checked, "ColorBrush2", "ColorBrush1"), "ColorBrushGray5"))
  208. End If
  209. End Sub
  210. Private Sub Radiobox_MouseEnterAnimation() Handles Me.MouseEnter
  211. AniStart(AaColor(ShapeBorder, Ellipse.StrokeProperty, "ColorBrush3", AnimationTimeOfMouseIn), "MyRadioBox BorderColor " & Uuid)
  212. AniStart(AaColor(LabText, TextBlock.ForegroundProperty, "ColorBrush3", AnimationTimeOfMouseIn), "MyRadioBox TextColor " & Uuid)
  213. End Sub
  214. Private Sub Radiobox_MouseLeaveAnimation() Handles Me.MouseLeave
  215. If Not Me.IsEnabled Then Return 'MouseLeave 比 IsEnabledChanged 后执行,所以如果自定义事件修改了 IsEnabled,将导致显示错误
  216. If IsLoaded AndAlso AniControlEnabled = 0 Then
  217. AniStart(AaColor(ShapeBorder, Ellipse.StrokeProperty, If(IsEnabled, If(Checked, "ColorBrush2", "ColorBrush1"), "ColorBrushGray5"), AnimationTimeOfMouseOut), "MyRadioBox BorderColor " & Uuid)
  218. AniStart(AaColor(LabText, TextBlock.ForegroundProperty, If(IsEnabled, "ColorBrush1", "ColorBrushGray5"), AnimationTimeOfMouseOut), "MyRadioBox TextColor " & Uuid)
  219. Else
  220. AniStop("MyRadioBox BorderColor " & Uuid)
  221. AniStop("MyRadioBox TextColor " & Uuid)
  222. ShapeBorder.SetResourceReference(Ellipse.StrokeProperty, If(IsEnabled, If(Checked, "ColorBrush2", "ColorBrush1"), "ColorBrushGray5"))
  223. LabText.SetResourceReference(TextBlock.ForegroundProperty, If(IsEnabled, "ColorBrush1", "ColorBrushGray5"))
  224. End If
  225. End Sub
  226. #Region "设置"
  227. Private Sub RefreshSetting(NewValue As String) Implements ISettingControl.RefreshSetting
  228. Checked = NewValue = SettingService.GetValue(Me)
  229. End Sub
  230. Private Function GetCurrentSetting() As String Implements ISettingControl.GetCurrentSetting
  231. Return If(Checked, SettingService.GetValue(Me), Nothing)
  232. End Function
  233. Private Sub SaveSetting() Handles Me.Check
  234. SettingService.SaveSetting(Me)
  235. End Sub
  236. #End Region
  237. End Class