MyComboBox.vb 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. Public Class MyComboBox
  2. Inherits ComboBox
  3. Public Event TextChanged(sender As Object, e As TextChangedEventArgs)
  4. '基础
  5. Public Uuid As Integer = GetUuid()
  6. Private TextBox As MyTextBox
  7. Public Property HintText As String = ""
  8. Public Overrides Sub OnApplyTemplate()
  9. MyBase.OnApplyTemplate()
  10. If Not IsEditable Then Exit Sub
  11. Try
  12. TextBox = Template.FindName("PART_EditableTextBox", Me)
  13. TextBox.AddHandler(LostFocusEvent, New RoutedEventHandler(AddressOf RefreshColor))
  14. TextBox.ChangedEventList.Add(New RoutedEventHandler(Sub(sender, e) RaiseEvent TextChanged(sender, e)))
  15. TextBox.Tag = Tag '有时需要用文本框的 Tag 来写入设置
  16. If Text = "" Then
  17. TextBox.Text = _Text
  18. Else
  19. RaiseEvent TextChanged(Me, Nothing)
  20. End If
  21. If HintText.Length > 0 Then TextBox.HintText = HintText
  22. Catch ex As Exception
  23. Log(ex, "初始化可编辑文本框失败(" & If(Name, "") & ")", LogLevel.Feedback)
  24. End Try
  25. End Sub
  26. Private _Text As String = SelectedItem
  27. Public Shadows Property Text As String
  28. Get
  29. If IsEditable Then
  30. If TextBox Is Nothing Then
  31. Return If(_Text, "")
  32. Else
  33. Return If(TextBox.Text, "")
  34. End If
  35. Else
  36. Return If(SelectedItem, "").ToString
  37. End If
  38. End Get
  39. Set(value As String)
  40. If IsEditable Then
  41. If IsNothing(TextBox) Then
  42. _Text = value
  43. Else
  44. TextBox.Text = value
  45. End If
  46. Else
  47. Throw New NotSupportedException("该 ComboBox 不支持修改文本。")
  48. End If
  49. End Set
  50. End Property
  51. '鼠标按下接口
  52. Private IsMouseDown As Boolean = False
  53. Private Sub MyComboBox_PreviewMouseLeftButtonDown(sender As Object, e As MouseButtonEventArgs) Handles Me.PreviewMouseLeftButtonDown
  54. IsMouseDown = True
  55. End Sub
  56. Private Sub MyComboBox_PreviewMouseLeftButtonUp(sender As Object, e As EventArgs) Handles Me.PreviewMouseLeftButtonUp, Me.MouseLeave
  57. IsMouseDown = False
  58. End Sub
  59. '指向动画
  60. Public Sub RefreshColor() Handles Me.IsEnabledChanged, Me.MouseEnter, Me.MouseLeave, Me.PreviewMouseLeftButtonDown, Me.PreviewMouseLeftButtonUp, Me.GotKeyboardFocus
  61. '判断当前颜色
  62. Dim ColorName As String
  63. Dim BackName As String
  64. Dim Time As Integer
  65. If IsEnabled Then
  66. If IsEditable AndAlso Template.FindName("PART_EditableTextBox", Me).IsFocused Then
  67. ColorName = "ColorBrush4"
  68. BackName = "ColorBrush9"
  69. Time = 60
  70. ElseIf IsMouseDown OrElse IsDropDownOpen Then
  71. ColorName = "ColorBrush3"
  72. BackName = "ColorBrush9"
  73. Time = 60
  74. ElseIf IsMouseOver Then
  75. ColorName = "ColorBrush3"
  76. BackName = "ColorBrushHalfWhite"
  77. Time = 100
  78. Else
  79. ColorName = "ColorBrush1"
  80. BackName = "ColorBrushHalfWhite"
  81. Time = 200
  82. End If
  83. Else
  84. ColorName = "ColorBrushGray4"
  85. BackName = "ColorBrushHalfWhite"
  86. Time = 200
  87. End If
  88. '触发颜色动画
  89. If IsLoaded AndAlso AniControlEnabled = 0 Then '防止默认属性变更触发动画
  90. '有动画
  91. AniStart({
  92. AaColor(Me, ForegroundProperty, ColorName, Time),
  93. AaColor(Me, BackgroundProperty, BackName, Time)
  94. }, "MyComboBox Color " & Uuid)
  95. Else
  96. '无动画
  97. AniStop("MyComboBox Color " & Uuid)
  98. SetResourceReference(ForegroundProperty, ColorName)
  99. SetResourceReference(BackgroundProperty, BackName)
  100. End If
  101. End Sub
  102. Private RealWidth As Double '由于下拉框 Popup 宽度与 Width 一致,故不能为 NaN(Auto)
  103. Private Sub MyComboBox_DropDownOpened(sender As Object, e As EventArgs) Handles Me.DropDownOpened
  104. RealWidth = Width
  105. Width = ActualWidth
  106. Try
  107. CType(Template.FindName("PanPopup", Me), Grid).Opacity = FrmMain.Opacity
  108. Catch ex As Exception
  109. Log(ex, "设置下拉框透明度失败", LogLevel.Feedback)
  110. End Try
  111. End Sub
  112. Private Sub MyComboBox_DropDownClosed(sender As Object, e As EventArgs) Handles Me.DropDownClosed
  113. Width = RealWidth
  114. End Sub
  115. '修复 WPF Bug:下拉框文本修改后,依然误认为还选择着此前的选项,导致再次点击该选项时内容不变
  116. Private IsTextChanging As Boolean = False
  117. Private Sub MyComboBox_TextChanged(sender As Object, e As TextChangedEventArgs) Handles Me.TextChanged
  118. If IsTextChanging OrElse Not IsEditable Then Exit Sub
  119. If SelectedItem IsNot Nothing AndAlso Text <> SelectedItem.ToString Then
  120. Dim RawText As String = Text
  121. Dim RawSelectionStart As Integer = TextBox.SelectionStart
  122. IsTextChanging = True
  123. SelectedItem = Nothing
  124. Text = RawText
  125. TextBox.SelectionStart = RawSelectionStart
  126. IsTextChanging = False
  127. End If
  128. End Sub
  129. End Class