MyListItem.xaml.vb 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687
  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.StartsWithF("http", True) 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.EndsWithF(".png", True) OrElse _Logo.EndsWithF(".jpg", True) 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.EndsWithF(".png", True) 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, _Half)
  327. End Set
  328. End Property
  329. Private _Half As Boolean = False
  330. Public Property Half As Boolean
  331. Get
  332. Return _Half
  333. End Get
  334. Set(value As Boolean)
  335. SetChecked(_Checked, False, True, value)
  336. End Set
  337. End Property
  338. ''' <summary>
  339. ''' 手动设置 Checked 属性。
  340. ''' </summary>
  341. ''' <param name="value">新的 Checked 属性。</param>
  342. ''' <param name="user">是否由用户引发。</param>
  343. ''' <param name="anime">是否执行动画。</param>
  344. ''' <param name="half">是否为部分选中。</param>
  345. Public Sub SetChecked(value As Boolean, user As Boolean, anime As Boolean, Optional half As Boolean = False)
  346. Try
  347. '自定义属性基础
  348. Dim ChangedEventArgs As New RouteEventArgs(user)
  349. Dim RawValue = _Checked
  350. If Type = CheckType.RadioBox Then
  351. If IsInitialized AndAlso Not value = _Checked Then
  352. _Checked = value
  353. RaiseEvent Changed(Me, ChangedEventArgs)
  354. If ChangedEventArgs.Handled Then
  355. _Checked = RawValue
  356. Exit Sub
  357. End If
  358. End If
  359. _Checked = value
  360. Else
  361. If value = _Checked AndAlso half = _Half Then Exit Sub
  362. _Checked = value
  363. _Half = half
  364. If IsInitialized Then
  365. RaiseEvent Changed(Me, ChangedEventArgs)
  366. If ChangedEventArgs.Handled Then
  367. _Checked = RawValue
  368. Exit Sub
  369. End If
  370. End If
  371. End If
  372. If value Then
  373. Dim CheckEventArgs As New RouteEventArgs(user)
  374. RaiseEvent Check(Me, CheckEventArgs)
  375. If CheckEventArgs.Handled Then Exit Sub
  376. End If
  377. '保证只有一个单选 ListItem 选中
  378. If Type = CheckType.RadioBox Then
  379. If IsNothing(Parent) Then Exit Sub
  380. Dim RadioboxList As New List(Of MyListItem)
  381. Dim CheckedCount As Integer = 0
  382. '收集控件列表与选中个数
  383. For Each Control In CType(Parent, Object).Children
  384. If TypeOf Control Is MyListItem AndAlso CType(Control, MyListItem).Type = CheckType.RadioBox Then
  385. RadioboxList.Add(Control)
  386. If Control.Checked Then CheckedCount += 1
  387. End If
  388. Next
  389. '判断选中情况
  390. Select Case CheckedCount
  391. Case 0
  392. '没有任何单选框被选中,选择第一个
  393. RadioboxList(0).Checked = True
  394. Case Is > 1
  395. '选中项目多于 1 个
  396. If Me.Checked Then
  397. '如果本控件选中,则取消其他所有控件的选中
  398. For Each Control As MyListItem In RadioboxList
  399. If Control.Checked AndAlso Not Control.Equals(Me) Then Control.Checked = False
  400. Next
  401. Else
  402. '如果本控件未选中,则只保留第一个选中的控件
  403. Dim FirstChecked = False
  404. For Each Control As MyListItem In RadioboxList
  405. If Control.Checked Then
  406. If FirstChecked Then
  407. Control.Checked = False '修改 Checked 会自动触发 Change 事件,所以不用额外触发
  408. Else
  409. FirstChecked = True
  410. End If
  411. End If
  412. Next
  413. End If
  414. End Select
  415. End If
  416. '更改动画
  417. If IsLoaded AndAlso AniControlEnabled = 0 AndAlso anime Then '防止默认属性变更触发动画
  418. Dim Anim As New List(Of AniData)
  419. If Checked Then
  420. '由无变有
  421. If Not IsNothing(RectCheck) Then
  422. Dim Delta = ActualHeight - RectCheck.ActualHeight - 12
  423. Anim.Add(AaHeight(RectCheck, Delta * If(half, 0.2, 0.4), 200,, New AniEaseOutFluent(AniEasePower.Weak)))
  424. Anim.Add(AaHeight(RectCheck, Delta * If(half, 0.3, 0.6), 300,, New AniEaseOutBack(AniEasePower.Weak)))
  425. Anim.Add(AaOpacity(RectCheck, If(half, 0.5, 1) - RectCheck.Opacity, 30))
  426. RectCheck.VerticalAlignment = VerticalAlignment.Center
  427. RectCheck.Margin = New Thickness(-1, 0, 0, 0)
  428. End If
  429. Anim.Add(AaColor(Me, ForegroundProperty, If(Height < 40, "ColorBrush3", "ColorBrush2"), 200))
  430. Else
  431. '由有变无
  432. If Not IsNothing(RectCheck) Then
  433. 'Anim.Add(AaWidth(RectCheck, -RectCheck.Width, 120,, New AniEaseInFluent))
  434. Anim.Add(AaHeight(RectCheck, -RectCheck.ActualHeight, 120,, New AniEaseInFluent(AniEasePower.Weak)))
  435. Anim.Add(AaOpacity(RectCheck, -RectCheck.Opacity, 70, 40))
  436. RectCheck.VerticalAlignment = VerticalAlignment.Center
  437. End If
  438. Anim.Add(AaColor(Me, ForegroundProperty, "ColorBrush1", 120))
  439. End If
  440. AniStart(Anim, "MyListItem Checked " & Uuid)
  441. Else
  442. '不使用动画
  443. AniStop("MyListItem Checked " & Uuid)
  444. If Checked OrElse half Then
  445. If Not IsNothing(RectCheck) Then
  446. RectCheck.Height = Double.NaN
  447. RectCheck.Margin = New Thickness(-1, 6, 0, 6)
  448. RectCheck.Opacity = 1
  449. RectCheck.VerticalAlignment = VerticalAlignment.Stretch
  450. If half Then
  451. RectCheck.Height = Height * 0.4
  452. RectCheck.Opacity = 0.5
  453. End If
  454. End If
  455. SetResourceReference(ForegroundProperty, If(Height < 40, "ColorBrush3", "ColorBrush2"))
  456. Else
  457. If Not IsNothing(RectCheck) Then
  458. RectCheck.Height = 0
  459. RectCheck.Margin = New Thickness(-1, 0, 0, 0)
  460. RectCheck.Opacity = 0
  461. RectCheck.VerticalAlignment = VerticalAlignment.Center
  462. End If
  463. SetResourceReference(ForegroundProperty, "ColorBrush1")
  464. End If
  465. End If
  466. Catch ex As Exception
  467. Log(ex, "设置 Checked 失败")
  468. End Try
  469. End Sub
  470. '前景色绑定
  471. Public Property Foreground As Brush
  472. Get
  473. Return GetValue(ForegroundProperty)
  474. End Get
  475. Set(value As Brush)
  476. SetValue(ForegroundProperty, value)
  477. End Set
  478. End Property
  479. Public Shared ReadOnly ForegroundProperty As DependencyProperty = DependencyProperty.Register("Foreground", GetType(Brush), GetType(MyListItem), New PropertyMetadata(CType(Color1, SolidColorBrush)))
  480. '菜单与按钮绑定
  481. Public ContentHandler As Action(Of MyListItem, EventArgs)
  482. #End Region
  483. #Region "点击"
  484. '触发点击事件
  485. Private Sub Button_MouseUp(sender As Object, e As MouseButtonEventArgs) Handles Me.PreviewMouseLeftButtonUp
  486. If Not IsMouseDown Then Exit Sub
  487. RaiseEvent Click(sender, e)
  488. If e.Handled Then Exit Sub
  489. '触发自定义事件
  490. If Not String.IsNullOrEmpty(EventType) Then
  491. ModEvent.TryStartEvent(EventType, EventData)
  492. e.Handled = True
  493. End If
  494. If e.Handled Then Exit Sub
  495. '实际的单击处理
  496. Select Case Type
  497. Case CheckType.Clickable
  498. Log("[Control] 按下单击列表项:" & Title)
  499. Case CheckType.RadioBox
  500. Log("[Control] 按下单选列表项:" & Title)
  501. If Not Checked Then SetChecked(True, True, True)
  502. Case CheckType.CheckBox
  503. Log("[Control] 按下复选列表项(" & (Not Checked).ToString & "):" & Title)
  504. SetChecked(Not Checked, True, True)
  505. End Select
  506. End Sub
  507. '鼠标点击判定
  508. Private IsMouseDown As Boolean = False
  509. Private Sub Button_MouseDown(sender As Object, e As MouseButtonEventArgs) Handles Me.PreviewMouseLeftButtonDown
  510. If IsMouseDirectlyOver AndAlso Not Type = CheckType.None Then
  511. IsMouseDown = True
  512. If ButtonStack IsNot Nothing Then ButtonStack.IsHitTestVisible = False
  513. End If
  514. End Sub
  515. Private Sub Button_MouseLeave(sender As Object, e As Object) Handles Me.MouseLeave, Me.PreviewMouseLeftButtonUp
  516. IsMouseDown = False
  517. If ButtonStack IsNot Nothing Then ButtonStack.IsHitTestVisible = True
  518. End Sub
  519. '实现自定义事件
  520. Public Property EventType As String
  521. Get
  522. Return GetValue(EventTypeProperty)
  523. End Get
  524. Set(value As String)
  525. SetValue(EventTypeProperty, value)
  526. End Set
  527. End Property
  528. Public Shared ReadOnly EventTypeProperty As DependencyProperty = DependencyProperty.Register("EventType", GetType(String), GetType(MyListItem), New PropertyMetadata(Nothing))
  529. Public Property EventData As String
  530. Get
  531. Return GetValue(EventDataProperty)
  532. End Get
  533. Set(value As String)
  534. SetValue(EventDataProperty, value)
  535. End Set
  536. End Property
  537. Public Shared ReadOnly EventDataProperty As DependencyProperty = DependencyProperty.Register("EventData", GetType(String), GetType(MyListItem), New PropertyMetadata(Nothing))
  538. #End Region
  539. Private StateLast As String
  540. Public IsMouseOverAnimationEnabled As Boolean = True
  541. Public Sub RefreshColor(sender As Object, e As EventArgs) Handles Me.MouseEnter, Me.MouseLeave, Me.MouseLeftButtonDown, Me.MouseLeftButtonUp
  542. '菜单虚拟化检测
  543. If ContentHandler IsNot Nothing Then
  544. ContentHandler(sender, e)
  545. ContentHandler = Nothing
  546. End If
  547. '判断当前颜色
  548. Dim StateNew As String, Time As Integer
  549. If IsMouseDown AndAlso Not (Type = CheckType.RadioBox AndAlso Checked) Then
  550. StateNew = "MouseDown"
  551. Time = 120
  552. Else
  553. If IsMouseOver AndAlso IsMouseOverAnimationEnabled Then
  554. StateNew = "MouseOver"
  555. Time = 120
  556. Else
  557. StateNew = "Idle"
  558. Time = 180
  559. End If
  560. End If
  561. If StateLast = StateNew Then Exit Sub
  562. StateLast = StateNew
  563. '触发颜色动画
  564. If IsLoaded AndAlso AniControlEnabled = 0 Then '防止默认属性变更触发动画
  565. '有动画
  566. Dim Ani As New List(Of AniData)
  567. If IsMouseOver AndAlso IsMouseOverAnimationEnabled Then
  568. If ButtonStack IsNot Nothing Then
  569. Ani.Add(AaOpacity(ButtonStack, 1 - ButtonStack.Opacity, Time * 0.7, Time * 0.3))
  570. Ani.Add(AaDouble(Sub(i) ColumnPaddingRight.Width = New GridLength(Math.Max(0, ColumnPaddingRight.Width.Value + i)),
  571. Math.Max(MinPaddingRight, 5 + Buttons.Count * 25) - ColumnPaddingRight.Width.Value, Time * 0.3, Time * 0.7))
  572. End If
  573. Ani.AddRange({
  574. AaColor(RectBack, Border.BackgroundProperty, If(IsMouseDown, "ColorBrush6", "ColorBrushBg1"), Time),
  575. AaOpacity(RectBack, 1 - RectBack.Opacity, Time,, New AniEaseOutFluent)
  576. })
  577. If IsScaleAnimationEnabled Then
  578. Ani.Add(AaScaleTransform(RectBack, 1 - CType(RectBack.RenderTransform, ScaleTransform).ScaleX, Time * 1.6,, New AniEaseOutFluent))
  579. If IsMouseDown Then
  580. Ani.Add(AaScaleTransform(Me, 0.98 - CType(Me.RenderTransform, ScaleTransform).ScaleX, Time * 0.9,, New AniEaseOutFluent))
  581. Else
  582. Ani.Add(AaScaleTransform(Me, 1 - CType(Me.RenderTransform, ScaleTransform).ScaleX, Time * 1.2,, New AniEaseOutFluent))
  583. End If
  584. End If
  585. Else
  586. If ButtonStack IsNot Nothing Then
  587. Ani.Add(AaOpacity(ButtonStack, -ButtonStack.Opacity, Time * 0.4))
  588. Ani.Add(AaDouble(Sub(i) ColumnPaddingRight.Width = New GridLength(Math.Max(0, ColumnPaddingRight.Width.Value + i)),
  589. MinPaddingRight - ColumnPaddingRight.Width.Value, Time * 0.4))
  590. End If
  591. Ani.Add(AaOpacity(RectBack, -RectBack.Opacity, Time))
  592. If IsScaleAnimationEnabled Then
  593. Ani.AddRange({
  594. AaColor(RectBack, Border.BackgroundProperty, If(IsMouseDown, "ColorBrush6", "ColorBrush7"), Time),
  595. AaScaleTransform(Me, 1 - CType(RenderTransform, ScaleTransform).ScaleX, Time * 3,, New AniEaseOutFluent),
  596. AaScaleTransform(RectBack, 0.996 - CType(RectBack.RenderTransform, ScaleTransform).ScaleX, Time,, New AniEaseOutFluent),
  597. AaScaleTransform(RectBack, -0.246, 1,,, True)
  598. })
  599. End If
  600. End If
  601. AniStart(Ani, "ListItem Color " & Uuid)
  602. Else
  603. '无动画
  604. If IsMouseOver AndAlso IsMouseOverAnimationEnabled Then
  605. If ButtonStack IsNot Nothing Then
  606. ButtonStack.Opacity = 1
  607. ColumnPaddingRight.Width = New GridLength(Math.Max(MinPaddingRight, 5 + Buttons.Count * 25))
  608. End If
  609. '由于鼠标已经移入,所以直接实例化 RectBack
  610. RectBack.Background = ColorBg1
  611. RectBack.Opacity = 1
  612. RectBack.RenderTransform = New ScaleTransform(1, 1)
  613. Me.RenderTransform = New ScaleTransform(1, 1)
  614. Else
  615. If ButtonStack IsNot Nothing Then
  616. ButtonStack.Opacity = 0
  617. ColumnPaddingRight.Width = New GridLength(MinPaddingRight)
  618. End If
  619. Me.RenderTransform = New ScaleTransform(1, 1)
  620. If _RectBack IsNot Nothing Then
  621. If IsScaleAnimationEnabled Then RectBack.RenderTransform = New ScaleTransform(0.75, 0.75)
  622. RectBack.Background = Color7
  623. RectBack.Opacity = 0
  624. End If
  625. End If
  626. AniStop("ListItem Color " & Uuid)
  627. End If
  628. End Sub
  629. Private Sub MyListItem_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
  630. If Checked Then
  631. SetResourceReference(ForegroundProperty, If(Height < 40, "ColorBrush3", "ColorBrush2"))
  632. Else
  633. SetResourceReference(ForegroundProperty, "ColorBrush1")
  634. End If
  635. ColumnPaddingRight.Width = New GridLength(MinPaddingRight)
  636. If EventType = "打开帮助" AndAlso Not (Title <> "" AndAlso Info <> "") Then '#3266
  637. Try
  638. Dim Unused = New HelpEntry(GetEventAbsoluteUrls(EventData, EventType)(0)).SetToListItem(Me)
  639. Catch ex As Exception
  640. Log(ex, "设置帮助 MyListItem 失败", LogLevel.Msgbox)
  641. End Try
  642. End If
  643. End Sub
  644. Public Overrides Function ToString() As String
  645. Return Title
  646. End Function
  647. End Class