MyListItem.xaml.vb 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671
  1. Public Class MyListItem
  2. Implements IMyRadio
  3. Public Event Click(sender As Object, e As MouseButtonEventArgs)
  4. Public Event LogoClick(sender As Object, e As MouseButtonEventArgs)
  5. Public Event Check(sender As Object, e As RouteEventArgs) Implements IMyRadio.Check
  6. Public Event Changed(sender As Object, e As RouteEventArgs) Implements IMyRadio.Changed
  7. #Region "后加载控件"
  8. '指向背景
  9. Private _RectBack As Border = Nothing
  10. Public ReadOnly Property RectBack As Border
  11. Get
  12. If _RectBack Is Nothing Then
  13. Dim Rect As New Border With {
  14. .Name = "RectBack",
  15. .CornerRadius = New CornerRadius(If(IsScaleAnimationEnabled OrElse Height > 40, 6, 0)),
  16. .RenderTransform = If(IsScaleAnimationEnabled, New ScaleTransform(0.8, 0.8), Nothing),
  17. .RenderTransformOrigin = New Point(0.5, 0.5),
  18. .BorderThickness = New Thickness(GetWPFSize(1)),
  19. .SnapsToDevicePixels = True,
  20. .IsHitTestVisible = False,
  21. .Opacity = 0
  22. }
  23. Rect.SetResourceReference(Border.BackgroundProperty, "ColorBrush7")
  24. Rect.SetResourceReference(Border.BorderBrushProperty, "ColorBrush6")
  25. SetColumnSpan(Rect, 999)
  26. SetRowSpan(Rect, 999)
  27. Children.Insert(0, Rect)
  28. _RectBack = Rect
  29. '<!--<Border x:Name = "RectBack" CornerRadius="3" RenderTransformOrigin="0.5,0.5" SnapsToDevicePixels="True"
  30. 'IsHitTestVisible = "False" Opacity="0" BorderThickness="1"
  31. 'Grid.ColumnSpan = "4" Background="{DynamicResource ColorBrush7}" BorderBrush="{DynamicResource ColorBrush6}"/>-->
  32. End If
  33. Return _RectBack
  34. End Get
  35. End Property
  36. '按钮
  37. Public ButtonStack As FrameworkElement
  38. '图标
  39. Public PathLogo As FrameworkElement
  40. '勾选条
  41. Public RectCheck As Border
  42. '副文本
  43. Private _LabInfo As TextBlock = Nothing
  44. Public ReadOnly Property LabInfo As TextBlock
  45. Get
  46. If _LabInfo Is Nothing Then
  47. Dim Lab As New TextBlock With {
  48. .Name = "LabInfo",
  49. .SnapsToDevicePixels = False,
  50. .UseLayoutRounding = False,
  51. .HorizontalAlignment = HorizontalAlignment.Left,
  52. .IsHitTestVisible = False,
  53. .TextTrimming = TextTrimming.CharacterEllipsis,
  54. .Visibility = Visibility.Collapsed,
  55. .FontSize = 12,
  56. .Margin = New Thickness(4, 0, 0, 0),
  57. .Opacity = 0.6
  58. }
  59. SetColumn(Lab, 3)
  60. SetRow(Lab, 2)
  61. Children.Add(Lab)
  62. _LabInfo = Lab
  63. '<TextBlock Grid.Row="2" SnapsToDevicePixels="False" UseLayoutRounding="False" HorizontalAlignment="Left" x:Name = "LabInfo" IsHitTestVisible="False" Grid.Column="2"
  64. 'TextTrimming = "CharacterEllipsis" Visibility="Collapsed" FontSize="12" Foreground="{StaticResource ColorBrushGray2}" Margin="4,0,0,0" />
  65. End If
  66. Return _LabInfo
  67. End Get
  68. End Property
  69. #End Region
  70. #Region "自定义属性"
  71. 'Uuid
  72. Public Uuid As Integer = GetUuid()
  73. ''' <summary>
  74. ''' 是否启用缩放动画。
  75. ''' </summary>
  76. Public Property IsScaleAnimationEnabled As Boolean
  77. Get
  78. Return _IsScaleAnimationEnabled
  79. End Get
  80. Set
  81. _IsScaleAnimationEnabled = Value
  82. If _RectBack IsNot Nothing Then RectBack.CornerRadius = New CornerRadius(If(Value, 6, 0))
  83. End Set
  84. End Property
  85. Private _IsScaleAnimationEnabled As Boolean = True
  86. '边距
  87. Public Property PaddingLeft As Integer
  88. Get
  89. Return ColumnPaddingLeft.Width.Value
  90. End Get
  91. Set(value As Integer)
  92. ColumnPaddingLeft.Width = New GridLength(value)
  93. End Set
  94. End Property
  95. ''' <summary>
  96. ''' 右边距的最小值。
  97. ''' 在存在右侧按钮时,右边距会被自动设置为 5 + 按钮数 * 25。
  98. ''' </summary>
  99. Public Property MinPaddingRight As Integer = 4
  100. '按钮
  101. Private _Buttons As IEnumerable(Of MyIconButton)
  102. Public Property Buttons As IEnumerable(Of MyIconButton)
  103. Get
  104. Return _Buttons
  105. End Get
  106. Set(value As IEnumerable(Of MyIconButton))
  107. _Buttons = value
  108. '没有特殊按钮,移除原 Stack
  109. If ButtonStack IsNot Nothing Then
  110. Children.Remove(ButtonStack)
  111. ButtonStack = Nothing
  112. End If
  113. '添加新 Stack
  114. Select Case value.Count
  115. Case 0
  116. '没有按钮,不添加新的
  117. Case 1
  118. '只有一个按钮
  119. For Each Btn As MyIconButton In value
  120. If Btn.Height.Equals(Double.NaN) Then Btn.Height = 25
  121. If Btn.Width.Equals(Double.NaN) Then Btn.Width = 25
  122. With Btn
  123. .Opacity = 0
  124. .Margin = New Thickness(0, 0, 5, 0)
  125. .SnapsToDevicePixels = False
  126. .HorizontalAlignment = HorizontalAlignment.Right
  127. .VerticalAlignment = VerticalAlignment.Center
  128. .SnapsToDevicePixels = False
  129. .UseLayoutRounding = False
  130. End With
  131. SetColumnSpan(Btn, 10) : SetRowSpan(Btn, 10)
  132. Children.Add(Btn)
  133. ButtonStack = Btn
  134. Next
  135. Case Else
  136. '有复数按钮,使用 StackPanel
  137. ButtonStack = New StackPanel With {.Opacity = 0, .Margin = New Thickness(0, 0, 5, 0), .SnapsToDevicePixels = False, .Orientation = Orientation.Horizontal, .HorizontalAlignment = HorizontalAlignment.Right, .VerticalAlignment = VerticalAlignment.Center, .UseLayoutRounding = False}
  138. SetColumnSpan(ButtonStack, 10) : SetRowSpan(ButtonStack, 10)
  139. '构造按钮
  140. For Each Btn As MyIconButton In value
  141. If Btn.Height.Equals(Double.NaN) Then Btn.Height = 25
  142. If Btn.Width.Equals(Double.NaN) Then Btn.Width = 25
  143. CType(ButtonStack, StackPanel).Children.Add(Btn)
  144. Next
  145. Children.Add(ButtonStack)
  146. End Select
  147. End Set
  148. End Property
  149. '标题
  150. Public Property Title As String
  151. Get
  152. Return GetValue(TitleProperty)
  153. End Get
  154. Set(value As String)
  155. SetValue(TitleProperty, value)
  156. End Set
  157. End Property
  158. Public Shared ReadOnly TitleProperty As DependencyProperty = DependencyProperty.Register("Title", GetType(String), GetType(MyListItem))
  159. '字号
  160. Public Property FontSize As Double
  161. Get
  162. Return GetValue(FontSizeProperty)
  163. End Get
  164. Set(value As Double)
  165. SetValue(FontSizeProperty, value)
  166. End Set
  167. End Property
  168. Public Shared ReadOnly FontSizeProperty As DependencyProperty = DependencyProperty.Register("FontSize", GetType(Double), GetType(MyListItem), New PropertyMetadata(CType(14, Double)))
  169. '信息
  170. Private _Info As String = ""
  171. Public Property Info As String
  172. Get
  173. Return _Info
  174. End Get
  175. Set(value As String)
  176. If _Info = value Then Exit Property
  177. value = value.Replace(vbCr, "").Replace(vbLf, "")
  178. _Info = value
  179. LabInfo.Text = value
  180. LabInfo.Visibility = If(value = "", Visibility.Collapsed, Visibility.Visible)
  181. End Set
  182. End Property
  183. '图片
  184. Private _Logo As String = ""
  185. Public Property Logo As String
  186. Get
  187. Return _Logo
  188. End Get
  189. Set(value As String)
  190. If _Logo = value Then Exit Property
  191. _Logo = value
  192. '删除旧 Logo
  193. If Not IsNothing(PathLogo) Then Children.Remove(PathLogo)
  194. '添加新 Logo
  195. If Not _Logo = "" Then
  196. If _Logo.ToLower.StartsWith("http") Then
  197. '网络图片
  198. PathLogo = New Image With {
  199. .Tag = Me,
  200. .IsHitTestVisible = LogoClickable,
  201. .Source = New ImageSourceConverter().ConvertFromString(_Logo),
  202. .RenderTransformOrigin = New Point(0.5, 0.5),
  203. .RenderTransform = New ScaleTransform With {.ScaleX = LogoScale, .ScaleY = LogoScale},
  204. .SnapsToDevicePixels = True, .UseLayoutRounding = False}
  205. RenderOptions.SetBitmapScalingMode(PathLogo, BitmapScalingMode.Linear)
  206. ElseIf _Logo.ToLower.EndsWith(".png") OrElse _Logo.ToLower.EndsWith(".jpg") Then
  207. '位图
  208. Dim Bitmap = New MyBitmap(_Logo)
  209. PathLogo = New Canvas With {
  210. .Tag = Me,
  211. .IsHitTestVisible = LogoClickable,
  212. .Background = Bitmap,
  213. .RenderTransformOrigin = New Point(0.5, 0.5),
  214. .RenderTransform = New ScaleTransform With {.ScaleX = LogoScale, .ScaleY = LogoScale},
  215. .SnapsToDevicePixels = True, .UseLayoutRounding = False}
  216. 'If Bitmap.Pic.Width = 16 AndAlso Bitmap.Pic.Height = 16 Then
  217. ' '使用最适合 16x16 物品图片显示的大小
  218. ' RenderOptions.SetBitmapScalingMode(PathLogo, BitmapScalingMode.NearestNeighbor)
  219. ' PathLogo.HorizontalAlignment = HorizontalAlignment.Center : PathLogo.VerticalAlignment = VerticalAlignment.Center
  220. ' PathLogo.Width = GetWPFSize(Math.Floor(GetPixelSize(32) / 16) * 16) : PathLogo.Height = PathLogo.Width
  221. 'Else
  222. PathLogo.HorizontalAlignment = HorizontalAlignment.Stretch : PathLogo.VerticalAlignment = VerticalAlignment.Stretch
  223. RenderOptions.SetBitmapScalingMode(PathLogo, BitmapScalingMode.HighQuality)
  224. 'End If
  225. Else
  226. '矢量图
  227. PathLogo = New Shapes.Path With {
  228. .Tag = Me,
  229. .IsHitTestVisible = LogoClickable, .HorizontalAlignment = HorizontalAlignment.Center, .VerticalAlignment = VerticalAlignment.Center, .Stretch = Stretch.Uniform,
  230. .Data = (New GeometryConverter).ConvertFromString(_Logo),
  231. .RenderTransformOrigin = New Point(0.5, 0.5),
  232. .RenderTransform = New ScaleTransform With {.ScaleX = LogoScale, .ScaleY = LogoScale},
  233. .SnapsToDevicePixels = False, .UseLayoutRounding = False}
  234. PathLogo.SetBinding(Shapes.Path.FillProperty, New Binding("Foreground") With {.Source = Me})
  235. End If
  236. SetColumn(PathLogo, 2)
  237. SetRowSpan(PathLogo, 4)
  238. OnSizeChanged() '设置边距
  239. Children.Add(PathLogo)
  240. '图标的点击事件
  241. If LogoClickable Then
  242. AddHandler PathLogo.MouseLeave, Sub(sender, e) IsLogoDown = False
  243. AddHandler PathLogo.MouseLeftButtonDown, Sub(sender, e) IsLogoDown = True
  244. AddHandler PathLogo.MouseLeftButtonUp, Sub(sender, e) If IsLogoDown Then IsLogoDown = False : RaiseEvent LogoClick(sender.Tag, e)
  245. End If
  246. End If
  247. '改变行距
  248. ColumnLogo.Width = New GridLength(If(_Logo = "", 0, 34) + If(Height < 40, 0, 4))
  249. End Set
  250. End Property
  251. Private _LogoScale As Double = 1
  252. Public Property LogoScale() As Double
  253. Get
  254. Return _LogoScale
  255. End Get
  256. Set(value As Double)
  257. _LogoScale = value
  258. If Not IsNothing(PathLogo) Then PathLogo.RenderTransform = New ScaleTransform With {.ScaleX = LogoScale, .ScaleY = LogoScale}
  259. End Set
  260. End Property
  261. '图标的点击
  262. ''' <summary>
  263. ''' 该 Logo 是否可用点击触发事件。需要在 Logo 属性之前设置。
  264. ''' </summary>
  265. Public Property LogoClickable As Boolean = False
  266. Private IsLogoDown As Boolean = False
  267. '勾选选项
  268. Public Enum CheckType
  269. None
  270. Clickable
  271. RadioBox
  272. CheckBox
  273. End Enum
  274. Private _Type As CheckType = CheckType.None
  275. Public Property Type As CheckType
  276. Get
  277. Return _Type
  278. End Get
  279. Set(value As CheckType)
  280. If _Type = value Then Exit Property
  281. _Type = value
  282. '切换左栏大小
  283. ColumnCheck.Width = New GridLength(If(_Type = CheckType.None OrElse _Type = CheckType.Clickable, If(Height < 40, 4, 2), 6))
  284. '切换竖条控件
  285. If _Type = CheckType.None OrElse _Type = CheckType.Clickable Then
  286. '移除竖条控件
  287. If Not IsNothing(RectCheck) Then
  288. Children.Remove(RectCheck)
  289. RectCheck = Nothing
  290. End If
  291. SetChecked(False, False, False)
  292. Else
  293. '添加竖条控件
  294. If IsNothing(RectCheck) Then
  295. RectCheck = New Border With {.Width = 5, .Height = If(Checked, Double.NaN, 0), .CornerRadius = New CornerRadius(2, 2, 2, 2),
  296. .VerticalAlignment = If(Checked, VerticalAlignment.Stretch, VerticalAlignment.Center),
  297. .HorizontalAlignment = HorizontalAlignment.Left, .UseLayoutRounding = False, .SnapsToDevicePixels = False,
  298. .Margin = If(Checked, New Thickness(-1, 6, 0, 6), New Thickness(-1, 0, 0, 0))}
  299. RectCheck.SetResourceReference(Border.BackgroundProperty, "ColorBrush3")
  300. SetRowSpan(RectCheck, 4)
  301. Children.Add(RectCheck)
  302. End If
  303. End If
  304. End Set
  305. End Property
  306. '适应尺寸
  307. Private Sub OnSizeChanged() Handles Me.SizeChanged
  308. ColumnCheck.Width = New GridLength(If(_Type = CheckType.None OrElse _Type = CheckType.Clickable, If(Height < 40, 4, 2), 6))
  309. ColumnLogo.Width = New GridLength(If(_Logo = "", 0, 34) + If(Height < 40, 0, 4))
  310. If Not IsNothing(PathLogo) Then
  311. If _Logo.EndsWith(".png") Then
  312. PathLogo.Margin = New Thickness(4, 5, 3, 5)
  313. Else
  314. PathLogo.Margin = New Thickness(If(Height < 40, 6, 8), 8, If(Height < 40, 4, 6), 8)
  315. End If
  316. End If
  317. LabTitle.Margin = New Thickness(4, 0, 0, If(Height < 40, 0, 2))
  318. End Sub
  319. '勾选状态
  320. Private _Checked As Boolean = False
  321. Public Property Checked As Boolean
  322. Get
  323. Return _Checked
  324. End Get
  325. Set(value As Boolean)
  326. SetChecked(value, False, True)
  327. End Set
  328. End Property
  329. ''' <summary>
  330. ''' 手动设置 Checked 属性。
  331. ''' </summary>
  332. ''' <param name="value">新的 Checked 属性。</param>
  333. ''' <param name="user">是否由用户引发。</param>
  334. ''' <param name="anime">是否执行动画。</param>
  335. Public Sub SetChecked(value As Boolean, user As Boolean, anime As Boolean)
  336. Try
  337. '自定义属性基础
  338. Dim ChangedEventArgs As New RouteEventArgs(user)
  339. Dim RawValue = _Checked
  340. If Type = CheckType.RadioBox Then
  341. If IsInitialized AndAlso Not value = _Checked Then
  342. _Checked = value
  343. RaiseEvent Changed(Me, ChangedEventArgs)
  344. If ChangedEventArgs.Handled Then
  345. _Checked = RawValue
  346. Exit Sub
  347. End If
  348. End If
  349. _Checked = value
  350. Else
  351. If value = _Checked Then Exit Sub
  352. _Checked = value
  353. If IsInitialized Then
  354. RaiseEvent Changed(Me, ChangedEventArgs)
  355. If ChangedEventArgs.Handled Then
  356. _Checked = RawValue
  357. Exit Sub
  358. End If
  359. End If
  360. End If
  361. If value Then
  362. Dim CheckEventArgs As New RouteEventArgs(user)
  363. RaiseEvent Check(Me, CheckEventArgs)
  364. If CheckEventArgs.Handled Then Exit Sub
  365. End If
  366. '保证只有一个单选 ListItem 选中
  367. If Type = CheckType.RadioBox Then
  368. If IsNothing(Parent) Then Exit Sub
  369. Dim RadioboxList As New List(Of MyListItem)
  370. Dim CheckedCount As Integer = 0
  371. '收集控件列表与选中个数
  372. For Each Control In CType(Parent, Object).Children
  373. If TypeOf Control Is MyListItem AndAlso CType(Control, MyListItem).Type = CheckType.RadioBox Then
  374. RadioboxList.Add(Control)
  375. If Control.Checked Then CheckedCount += 1
  376. End If
  377. Next
  378. '判断选中情况
  379. Select Case CheckedCount
  380. Case 0
  381. '没有任何单选框被选中,选择第一个
  382. RadioboxList(0).Checked = True
  383. Case Is > 1
  384. '选中项目多于 1 个
  385. If Me.Checked Then
  386. '如果本控件选中,则取消其他所有控件的选中
  387. For Each Control As MyListItem In RadioboxList
  388. If Control.Checked AndAlso Not Control.Equals(Me) Then Control.Checked = False
  389. Next
  390. Else
  391. '如果本控件未选中,则只保留第一个选中的控件
  392. Dim FirstChecked = False
  393. For Each Control As MyListItem In RadioboxList
  394. If Control.Checked Then
  395. If FirstChecked Then
  396. Control.Checked = False '修改 Checked 会自动触发 Change 事件,所以不用额外触发
  397. Else
  398. FirstChecked = True
  399. End If
  400. End If
  401. Next
  402. End If
  403. End Select
  404. End If
  405. '更改动画
  406. If IsLoaded AndAlso AniControlEnabled = 0 AndAlso anime Then '防止默认属性变更触发动画
  407. Dim Anim As New List(Of AniData)
  408. If Checked Then
  409. '由无变有
  410. If Not IsNothing(RectCheck) Then
  411. Dim Delta = ActualHeight - RectCheck.ActualHeight - 12
  412. Anim.Add(AaHeight(RectCheck, Delta * 0.4, 200,, New AniEaseOutFluent(AniEasePower.Weak)))
  413. Anim.Add(AaHeight(RectCheck, Delta * 0.6, 300,, New AniEaseOutBack(AniEasePower.Weak)))
  414. Anim.Add(AaOpacity(RectCheck, 1 - RectCheck.Opacity, 30))
  415. RectCheck.VerticalAlignment = VerticalAlignment.Center
  416. RectCheck.Margin = New Thickness(-1, 0, 0, 0)
  417. End If
  418. Anim.Add(AaColor(Me, ForegroundProperty, If(Height < 40, "ColorBrush3", "ColorBrush2"), 200))
  419. Else
  420. '由有变无
  421. If Not IsNothing(RectCheck) Then
  422. 'Anim.Add(AaWidth(RectCheck, -RectCheck.Width, 120,, New AniEaseInFluent))
  423. Anim.Add(AaHeight(RectCheck, -RectCheck.ActualHeight, 120,, New AniEaseInFluent(AniEasePower.Weak)))
  424. Anim.Add(AaOpacity(RectCheck, -RectCheck.Opacity, 70, 40))
  425. RectCheck.VerticalAlignment = VerticalAlignment.Center
  426. End If
  427. Anim.Add(AaColor(Me, ForegroundProperty, "ColorBrush1", 120))
  428. End If
  429. AniStart(Anim, "MyListItem Checked " & Uuid)
  430. Else
  431. '不使用动画
  432. AniStop("MyListItem Checked " & Uuid)
  433. If Checked Then
  434. If Not IsNothing(RectCheck) Then
  435. RectCheck.Height = Double.NaN
  436. RectCheck.Margin = New Thickness(-1, 6, 0, 6)
  437. RectCheck.Opacity = 1
  438. RectCheck.VerticalAlignment = VerticalAlignment.Stretch
  439. End If
  440. SetResourceReference(ForegroundProperty, If(Height < 40, "ColorBrush3", "ColorBrush2"))
  441. Else
  442. If Not IsNothing(RectCheck) Then
  443. RectCheck.Height = 0
  444. RectCheck.Margin = New Thickness(-1, 0, 0, 0)
  445. RectCheck.Opacity = 0
  446. RectCheck.VerticalAlignment = VerticalAlignment.Center
  447. End If
  448. SetResourceReference(ForegroundProperty, "ColorBrush1")
  449. End If
  450. End If
  451. Catch ex As Exception
  452. Log(ex, "设置 Checked 失败")
  453. End Try
  454. End Sub
  455. '前景色绑定
  456. Public Property Foreground As Brush
  457. Get
  458. Return GetValue(ForegroundProperty)
  459. End Get
  460. Set(value As Brush)
  461. SetValue(ForegroundProperty, value)
  462. End Set
  463. End Property
  464. Public Shared ReadOnly ForegroundProperty As DependencyProperty = DependencyProperty.Register("Foreground", GetType(Brush), GetType(MyListItem), New PropertyMetadata(CType(Color1, SolidColorBrush)))
  465. '菜单与按钮绑定
  466. Public ContentHandler As Action(Of MyListItem, EventArgs)
  467. #End Region
  468. #Region "点击"
  469. '触发点击事件
  470. Private Sub Button_MouseUp(sender As Object, e As MouseButtonEventArgs) Handles Me.PreviewMouseLeftButtonUp
  471. If Not IsMouseDown Then Exit Sub
  472. RaiseEvent Click(sender, e)
  473. If e.Handled Then Exit Sub
  474. '触发自定义事件
  475. If Not String.IsNullOrEmpty(EventType) Then
  476. ModEvent.TryStartEvent(EventType, EventData)
  477. e.Handled = True
  478. End If
  479. If e.Handled Then Exit Sub
  480. '实际的单击处理
  481. Select Case Type
  482. Case CheckType.Clickable
  483. Log("[Control] 按下单击列表项:" & Title)
  484. Case CheckType.RadioBox
  485. Log("[Control] 按下单选列表项:" & Title)
  486. If Not Checked Then SetChecked(True, True, True)
  487. Case CheckType.CheckBox
  488. Log("[Control] 按下复选列表项(" & (Not Checked).ToString & "):" & Title)
  489. SetChecked(Not Checked, True, True)
  490. End Select
  491. End Sub
  492. '鼠标点击判定
  493. Private IsMouseDown As Boolean = False
  494. Private Sub Button_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles Me.PreviewMouseLeftButtonDown
  495. If IsMouseDirectlyOver AndAlso Not Type = CheckType.None Then
  496. IsMouseDown = True
  497. If ButtonStack IsNot Nothing Then ButtonStack.IsHitTestVisible = False
  498. End If
  499. End Sub
  500. Private Sub Button_MouseLeave(sender As Object, e As Object) Handles Me.MouseLeave, Me.PreviewMouseLeftButtonUp
  501. IsMouseDown = False
  502. If ButtonStack IsNot Nothing Then ButtonStack.IsHitTestVisible = True
  503. End Sub
  504. '实现自定义事件
  505. Public Property EventType As String
  506. Get
  507. Return GetValue(EventTypeProperty)
  508. End Get
  509. Set(value As String)
  510. SetValue(EventTypeProperty, value)
  511. End Set
  512. End Property
  513. Public Shared ReadOnly EventTypeProperty As DependencyProperty = DependencyProperty.Register("EventType", GetType(String), GetType(MyListItem), New PropertyMetadata(Nothing))
  514. Public Property EventData As String
  515. Get
  516. Return GetValue(EventDataProperty)
  517. End Get
  518. Set(value As String)
  519. SetValue(EventDataProperty, value)
  520. End Set
  521. End Property
  522. Public Shared ReadOnly EventDataProperty As DependencyProperty = DependencyProperty.Register("EventData", GetType(String), GetType(MyListItem), New PropertyMetadata(Nothing))
  523. #End Region
  524. Private StateLast As String
  525. Public IsMouseOverAnimationEnabled As Boolean = True
  526. Public Sub RefreshColor(sender As Object, e As EventArgs) Handles Me.MouseEnter, Me.MouseLeave, Me.MouseLeftButtonDown, Me.MouseLeftButtonUp
  527. '菜单虚拟化检测
  528. If ContentHandler IsNot Nothing Then
  529. ContentHandler(sender, e)
  530. ContentHandler = Nothing
  531. End If
  532. '判断当前颜色
  533. Dim StateNew As String, Time As Integer
  534. If IsMouseDown AndAlso Not (Type = CheckType.RadioBox AndAlso Checked) Then
  535. StateNew = "MouseDown"
  536. Time = 120
  537. Else
  538. If IsMouseOver AndAlso IsMouseOverAnimationEnabled Then
  539. StateNew = "MouseOver"
  540. Time = 120
  541. Else
  542. StateNew = "Idle"
  543. Time = 180
  544. End If
  545. End If
  546. If StateLast = StateNew Then Exit Sub
  547. StateLast = StateNew
  548. '触发颜色动画
  549. If IsLoaded AndAlso AniControlEnabled = 0 Then '防止默认属性变更触发动画
  550. '有动画
  551. Dim Ani As New List(Of AniData)
  552. If IsMouseOver AndAlso IsMouseOverAnimationEnabled Then
  553. If ButtonStack IsNot Nothing Then
  554. Ani.Add(AaOpacity(ButtonStack, 1 - ButtonStack.Opacity, Time * 0.7, Time * 0.3))
  555. Ani.Add(AaDouble(Sub(i) ColumnPaddingRight.Width = New GridLength(Math.Max(0, ColumnPaddingRight.Width.Value + i)),
  556. Math.Max(MinPaddingRight, 5 + Buttons.Count * 25) - ColumnPaddingRight.Width.Value, Time * 0.3, Time * 0.7))
  557. End If
  558. Ani.AddRange({
  559. AaColor(RectBack, Border.BackgroundProperty, If(IsMouseDown, "ColorBrush6", "ColorBrushBg1"), Time),
  560. AaOpacity(RectBack, 1 - RectBack.Opacity, Time,, New AniEaseOutFluent)
  561. })
  562. If IsScaleAnimationEnabled Then
  563. Ani.Add(AaScaleTransform(RectBack, 1 - CType(RectBack.RenderTransform, ScaleTransform).ScaleX, Time * 1.6,, New AniEaseOutFluent))
  564. If IsMouseDown Then
  565. Ani.Add(AaScaleTransform(Me, 0.98 - CType(Me.RenderTransform, ScaleTransform).ScaleX, Time * 0.9,, New AniEaseOutFluent))
  566. Else
  567. Ani.Add(AaScaleTransform(Me, 1 - CType(Me.RenderTransform, ScaleTransform).ScaleX, Time * 1.2,, New AniEaseOutFluent))
  568. End If
  569. End If
  570. Else
  571. If ButtonStack IsNot Nothing Then
  572. Ani.Add(AaOpacity(ButtonStack, -ButtonStack.Opacity, Time * 0.4))
  573. Ani.Add(AaDouble(Sub(i) ColumnPaddingRight.Width = New GridLength(Math.Max(0, ColumnPaddingRight.Width.Value + i)),
  574. MinPaddingRight - ColumnPaddingRight.Width.Value, Time * 0.4))
  575. End If
  576. Ani.Add(AaOpacity(RectBack, -RectBack.Opacity, Time))
  577. If IsScaleAnimationEnabled Then
  578. Ani.AddRange({
  579. AaColor(RectBack, Border.BackgroundProperty, If(IsMouseDown, "ColorBrush6", "ColorBrush7"), Time),
  580. AaScaleTransform(Me, 1 - CType(RenderTransform, ScaleTransform).ScaleX, Time * 3,, New AniEaseOutFluent),
  581. AaScaleTransform(RectBack, 0.996 - CType(RectBack.RenderTransform, ScaleTransform).ScaleX, Time,, New AniEaseOutFluent),
  582. AaScaleTransform(RectBack, -0.246, 1,,, True)
  583. })
  584. End If
  585. End If
  586. AniStart(Ani, "ListItem Color " & Uuid)
  587. Else
  588. '无动画
  589. If IsMouseOver AndAlso IsMouseOverAnimationEnabled Then
  590. If ButtonStack IsNot Nothing Then
  591. ButtonStack.Opacity = 1
  592. ColumnPaddingRight.Width = New GridLength(Math.Max(MinPaddingRight, 5 + Buttons.Count * 25))
  593. End If
  594. '由于鼠标已经移入,所以直接实例化 RectBack
  595. RectBack.Background = ColorBg1
  596. RectBack.Opacity = 1
  597. RectBack.RenderTransform = New ScaleTransform(1, 1)
  598. Me.RenderTransform = New ScaleTransform(1, 1)
  599. Else
  600. If ButtonStack IsNot Nothing Then
  601. ButtonStack.Opacity = 0
  602. ColumnPaddingRight.Width = New GridLength(MinPaddingRight)
  603. End If
  604. Me.RenderTransform = New ScaleTransform(1, 1)
  605. If _RectBack IsNot Nothing Then
  606. If IsScaleAnimationEnabled Then RectBack.RenderTransform = New ScaleTransform(0.75, 0.75)
  607. RectBack.Background = Color7
  608. RectBack.Opacity = 0
  609. End If
  610. End If
  611. AniStop("ListItem Color " & Uuid)
  612. End If
  613. End Sub
  614. Private Sub MyListItem_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
  615. If Checked Then
  616. SetResourceReference(ForegroundProperty, If(Height < 40, "ColorBrush3", "ColorBrush2"))
  617. Else
  618. SetResourceReference(ForegroundProperty, "ColorBrush1")
  619. End If
  620. ColumnPaddingRight.Width = New GridLength(MinPaddingRight)
  621. If EventType = "打开帮助" Then
  622. Try
  623. Dim NewElem = New HelpEntry(GetEventAbsoluteUrls(EventData, EventType)(0)).SetToListItem(Me)
  624. Catch ex As Exception
  625. Log(ex, "设置帮助 MyListItem 失败", LogLevel.Msgbox)
  626. End Try
  627. End If
  628. End Sub
  629. Public Overrides Function ToString() As String
  630. Return Title
  631. End Function
  632. End Class