基于JudaHimango对WinForms的回答。这不是完美的,但它的工作很好,可以使用。
Making a WinForms TextBox behave like your browser's address bar
Public Class GenericTextboxBehavior : Inherits Behavior(Of Windows.Controls.TextBox)
Private WithEvents m_Target As TextBox
Private alreadyFocused As Boolean
Protected Overrides Sub OnAttached()
If Not DesignerProperties.GetIsInDesignMode(AssociatedObject) Then
m_Target = AssociatedObject
End If
End Sub
Protected Overrides Sub OnDetaching()
If Not DesignerProperties.GetIsInDesignMode(AssociatedObject) Then
m_Target = Nothing
End If
End Sub
Private Sub m_Target_GotFocus(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles m_Target.GotFocus
Debug.WriteLine("LeftButton: " & Input.Mouse.LeftButton.ToString)
Debug.WriteLine("MiddleButton: " & Input.Mouse.MiddleButton.ToString)
Debug.WriteLine("RightButton: " & Input.Mouse.RightButton.ToString)
Debug.WriteLine("XButton1: " & Input.Mouse.XButton1.ToString)
Debug.WriteLine("XButton2: " & Input.Mouse.XButton2.ToString)
If Input.Mouse.LeftButton = MouseButtonState.Released And Input.Mouse.MiddleButton = MouseButtonState.Released And Input.Mouse.RightButton = MouseButtonState.Released And Input.Mouse.XButton1 = MouseButtonState.Released And Mouse.XButton2 = MouseButtonState.Released Then
m_Target.SelectAll()
alreadyFocused = True
Debug.WriteLine("GotFocus --> Select All")
Else
Debug.WriteLine("GotFocus " & alreadyFocused)
End If
End Sub
Private Sub m_Target_LostFocus(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles m_Target.LostFocus
alreadyFocused = False
Debug.WriteLine("LostFocus " & alreadyFocused)
End Sub
Private Sub m_Target_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Input.MouseButtonEventArgs) Handles m_Target.PreviewMouseUp
If Not alreadyFocused And m_Target.SelectionLength = 0 Then
alreadyFocused = True
m_Target.SelectAll()
Debug.WriteLine("MouseUp --> Select All")
Else
Debug.WriteLine("MouseUp " & alreadyFocused)
End If
End Sub
End Class
编辑
#Region "Boilerplate for XAML Attached Properties"
Public Shared IsEnabledProperty As DependencyProperty = DependencyProperty.RegisterAttached("IsEnabled", GetType(Boolean), GetType(SelectAllTextboxBehavior), New FrameworkPropertyMetadata(False, AddressOf OnIsEnabledChanged))
Public Shared Function GetIsEnabled(ByVal uie As DependencyObject) As Boolean
Return CBool(uie.GetValue(IsEnabledProperty))
End Function
Public Shared Sub SetIsEnabled(ByVal uie As DependencyObject, ByVal value As Boolean)
uie.SetValue(IsEnabledProperty, value)
End Sub
Public Shared Sub OnIsEnabledChanged(ByVal dpo As DependencyObject, ByVal e As DependencyPropertyChangedEventArgs)
Dim uie = TryCast(dpo, UIElement)
If uie Is Nothing Then Return
Dim behaviors = Interaction.GetBehaviors(uie)
Dim existingBehavior = TryCast(behaviors.FirstOrDefault(Function(b) b.GetType() = GetType(SelectAllTextboxBehavior)), SelectAllTextboxBehavior)
If CBool(e.NewValue) = False And existingBehavior IsNot Nothing Then
behaviors.Remove(existingBehavior)
Else
behaviors.Add(New SelectAllTextboxBehavior)
End If
End Sub
#End Region