自动选择 WPF TextBox 中焦点上的所有文本 [英] Automatically select all text on focus in WPF TextBox
本文介绍了自动选择 WPF TextBox 中焦点上的所有文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 WPF TextBoxes 中自动选择所有焦点文本?
How do you automatically select all text on focus in WPF TextBoxes?
推荐答案
基于 Judah Himango 对 WinForms 的回答.这并不完美,但它可以很好地使用.
Based on Judah Himango's answer for WinForms. This isn't perfect, but its works well enough to use.
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
这篇关于自动选择 WPF TextBox 中焦点上的所有文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文