如何在WPF TextBox中自动选择焦点上的所有文本?

Ser*_*hov 210 .net silverlight wpf textbox

如果我SelectAllGotFocus事件处理程序调用它,它对鼠标不起作用 - 一旦释放鼠标,选择就会消失.

编辑:人们都喜欢Donnelle的回答,我会试着解释为什么我不喜欢它和接受的答案一样多.

  • 它更复杂,而接受的答案以更简单的方式做同样的事情.
  • 接受答案的可用性更好.当您单击文本中间时,释放鼠标时文本将被取消选中,允许您立即开始编辑,如果您仍想选择全部,只需再次按下按钮,这次它将不会在发布时取消选择.按照Donelle的配方,如果我在文本中间单击,我必须单击第二次才能编辑.如果我单击文本中的某个位置而不是文本外部,这很可能意味着我想开始编辑而不是覆盖所有内容.

Don*_*lle 203

我们有它所以第一次点击选择全部,另一次点击进入光标(我们的应用程序设计用于带有笔的平板电脑).

你可能会发现它很有用.

public class ClickSelectTextBox : TextBox
{
    public ClickSelectTextBox()
    {
        AddHandler(PreviewMouseLeftButtonDownEvent, 
          new MouseButtonEventHandler(SelectivelyIgnoreMouseButton), true);
        AddHandler(GotKeyboardFocusEvent, 
          new RoutedEventHandler(SelectAllText), true);
        AddHandler(MouseDoubleClickEvent, 
          new RoutedEventHandler(SelectAllText), true);
    }

    private static void SelectivelyIgnoreMouseButton(object sender, 
                                                     MouseButtonEventArgs e)
    {
        // Find the TextBox
        DependencyObject parent = e.OriginalSource as UIElement;
        while (parent != null && !(parent is TextBox))
            parent = VisualTreeHelper.GetParent(parent);

        if (parent != null)
        {
            var textBox = (TextBox)parent;
            if (!textBox.IsKeyboardFocusWithin)
            {
                // If the text box is not yet focussed, give it the focus and
                // stop further processing of this click event.
                textBox.Focus();
                e.Handled = true;
            }
        }
    }

    private static void SelectAllText(object sender, RoutedEventArgs e)
    {
        var textBox = e.OriginalSource as TextBox;
        if (textBox != null)
            textBox.SelectAll();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 非常感谢你.这非常有效,应该是接受的答案恕我直言.当TextBox通过键盘或鼠标(显然是手写笔)获得焦点时,上面的代码可以正常工作.+1 (9认同)
  • 我在http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/564b5731-af8a-49bf-b297-6d179615819f/看到一个几乎相同的答案,它也有效,但它怎么样? t使用e.OriginalSource,也不爬行可视化树.做这一切有什么好处吗? (4认同)
  • 此解决方案的另一个缺点是当您使用TextBox的"剪切/复制/粘贴"菜单时,选择任何菜单项时将选择整个文本. (2认同)

Gro*_*kys 154

Donnelle的答案效果最好,但是必须派出一个新课程才能使用它是一种痛苦.

而不是这样做我在App.xaml.cs中为处理程序中的处理程序注册了应用程序中的所有TextBox.这允许我使用标准TextBox控件的Donnelle答案.

将以下方法添加到App.xaml.cs:

public partial class App : Application
{
    protected override void OnStartup(StartupEventArgs e) 
    {
        // Select the text in a TextBox when it receives focus.
        EventManager.RegisterClassHandler(typeof(TextBox), TextBox.PreviewMouseLeftButtonDownEvent,
            new MouseButtonEventHandler(SelectivelyIgnoreMouseButton));
        EventManager.RegisterClassHandler(typeof(TextBox), TextBox.GotKeyboardFocusEvent, 
            new RoutedEventHandler(SelectAllText));
        EventManager.RegisterClassHandler(typeof(TextBox), TextBox.MouseDoubleClickEvent,
            new RoutedEventHandler(SelectAllText));
        base.OnStartup(e); 
    }

    void SelectivelyIgnoreMouseButton(object sender, MouseButtonEventArgs e)
    {
        // Find the TextBox
        DependencyObject parent = e.OriginalSource as UIElement;
        while (parent != null && !(parent is TextBox))
            parent = VisualTreeHelper.GetParent(parent);

        if (parent != null)
        {
            var textBox = (TextBox)parent;
            if (!textBox.IsKeyboardFocusWithin)
            {
                // If the text box is not yet focused, give it the focus and
                // stop further processing of this click event.
                textBox.Focus();
                e.Handled = true;
            }
        }
    }

    void SelectAllText(object sender, RoutedEventArgs e)
    {
        var textBox = e.OriginalSource as TextBox;
        if (textBox != null)
            textBox.SelectAll();
    }
}
Run Code Online (Sandbox Code Playgroud)

  • 这是一个非常酷的解决方案,Matt Hamilton在很久以前也在这里描述过:http://madprops.org/blog/wpf-textbox-selectall-on-focus/ (4认同)
  • "在美国,拼写的拼写更为常见;但是,拼写的拼写有时在英国和加拿大使用,在澳大利亚和新西兰尤为常见." 所以nyah;) (3认同)
  • 谢谢Nate,纠正了,虽然在我的辩护中,我想指出拼写错误是从Donnelle的回答中逐字复制的;) (2认同)
  • 粉丝——太棒了——太美味了。这次真是万分感谢。我希望我能对此点赞 255 次。 (2认同)

Nil*_*ils 72

这是相当古老的,但无论如何我都会展示我的答案.
我选择了Donnelle的部分答案(跳过双击),因为我认为这会给用户带来最小的惊讶.但是,像gcores一样,我不喜欢创建派生类.但我也不喜欢gcores"on Startup ..."方法.我需要这个"一般但不总是"的基础.

我已将此实现为附加依赖项属性,因此我可以SelectTextOnFocus.Active=True在xaml中设置.我发现这种方式最令人愉悦.

namespace foo.styles.behaviour
{
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Input;
    using System.Windows.Media;

    public class SelectTextOnFocus : DependencyObject
    {
        public static readonly DependencyProperty ActiveProperty = DependencyProperty.RegisterAttached(
            "Active",
            typeof(bool),
            typeof(SelectTextOnFocus),
            new PropertyMetadata(false, ActivePropertyChanged));

        private static void ActivePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            if (d is TextBox)
            {
                TextBox textBox = d as TextBox;
                if ((e.NewValue as bool?).GetValueOrDefault(false))
                {
                    textBox.GotKeyboardFocus += OnKeyboardFocusSelectText;
                    textBox.PreviewMouseLeftButtonDown += OnMouseLeftButtonDown;
                }
                else
                {
                    textBox.GotKeyboardFocus -= OnKeyboardFocusSelectText;
                    textBox.PreviewMouseLeftButtonDown -= OnMouseLeftButtonDown;
                }
            }
        }

        private static void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DependencyObject dependencyObject = GetParentFromVisualTree(e.OriginalSource);

            if (dependencyObject == null)
            {
                return;
            }

            var textBox = (TextBox)dependencyObject;
            if (!textBox.IsKeyboardFocusWithin)
            {
                textBox.Focus();
                e.Handled = true;
            }
        }

        private static DependencyObject GetParentFromVisualTree(object source)
        {
            DependencyObject parent = source as UIElement;
            while (parent != null && !(parent is TextBox))
            {
                parent = VisualTreeHelper.GetParent(parent);
            }

            return parent;
        }

        private static void OnKeyboardFocusSelectText(object sender, KeyboardFocusChangedEventArgs e)
        {
            TextBox textBox = e.OriginalSource as TextBox;
            if (textBox != null)
            {
                textBox.SelectAll();
            }
        }

        [AttachedPropertyBrowsableForChildrenAttribute(IncludeDescendants = false)]
        [AttachedPropertyBrowsableForType(typeof(TextBox))]
        public static bool GetActive(DependencyObject @object)
        {
            return (bool) @object.GetValue(ActiveProperty);
        }

        public static void SetActive(DependencyObject @object, bool value)
        {
            @object.SetValue(ActiveProperty, value);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

对于我的"一般但不总是"特征,我在(全局)TextBox-Style中将此属性设置为True.这种方式"选择文本"总是"打开",但我可以在每个文本框的基础上禁用它.

  • +1这比全局设置要好得多,而且它比从TextBox派生的更像是"WPF方式". (7认同)
  • +1同意stijn.在app.cs中"隐藏"你的代码对于那些必须弄清楚为什么会发生SelectAllOnFocus的可怜开发者来说并不好.:-)我刚把它放到我的类TextBoxBehaviors中,然后更新了我的基本TextBox样式.做了一个享受.干杯 (3认同)
  • 另一个+1以获得最佳答案。我发现的唯一问题是,即使我使用鼠标右键,文本也总是被选中——我经常这样做以通过上下文菜单编辑文本——该解决方案不适用于这种情况,因为它总是选择所有文本,即使我只想通过上下文菜单剪切 1 个单词。你们知道如何解决这个问题吗? (3认同)
  • @tronda:只需使用TextBox的TargetType为资源添加样式.我建议你看看http://wpftutorial.net/Styles.html (2认同)
  • 我喜欢这个答案,但为什么要扩展 DependencyObject?我删除了它,它仍然可以正常工作。 (2认同)

gco*_*res 71

不知道为什么它在GotFocus事件中失去了选择.

但一种解决方案是在GotKeyboardFocus和GotMouseCapture事件上进行选择.这样它总能奏效.

  • 不.在现有文本中间用鼠标单击时 - 只要释放鼠标按钮,选择就会丢失. (9认同)
  • 虽然 - 在第二次单击之后,它再次选择所有文本...不确定它是否是来自WPF设计者的预期行为,但可用性并不是那么糟糕.与单个GotFocus处理程序的另一个区别是单击TextBox中的空白区域会选择全部. (3认同)
  • 这也是我的第一个解决方案.但我发现当用户无法使用鼠标选择文本时,用户真的很烦,因为每次点击整个文本都会被选中... (3认同)

Ser*_*hov 41

以下是实现答案解决方案的Blend行为,以方便您:

一个用于附加到单个TextBox:

public class SelectAllTextOnFocusBehavior : Behavior<TextBox>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.GotKeyboardFocus += AssociatedObjectGotKeyboardFocus;
        AssociatedObject.GotMouseCapture += AssociatedObjectGotMouseCapture;
        AssociatedObject.PreviewMouseLeftButtonDown += AssociatedObjectPreviewMouseLeftButtonDown;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.GotKeyboardFocus -= AssociatedObjectGotKeyboardFocus;
        AssociatedObject.GotMouseCapture -= AssociatedObjectGotMouseCapture;
        AssociatedObject.PreviewMouseLeftButtonDown -= AssociatedObjectPreviewMouseLeftButtonDown;
    }

    private void AssociatedObjectGotKeyboardFocus(object sender,
        System.Windows.Input.KeyboardFocusChangedEventArgs e)
    {
        AssociatedObject.SelectAll();
    }

    private void AssociatedObjectGotMouseCapture(object sender,
        System.Windows.Input.MouseEventArgs e)
    {
        AssociatedObject.SelectAll();   
    }

    private void AssociatedObjectPreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        if(!AssociatedObject.IsKeyboardFocusWithin)
        {
            AssociatedObject.Focus();
            e.Handled = true;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

一个用于附加到包含多个TextBox'es的容器的根目录:

public class SelectAllTextOnFocusMultiBehavior : Behavior<UIElement>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.GotKeyboardFocus += HandleKeyboardFocus;
        AssociatedObject.GotMouseCapture += HandleMouseCapture;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.GotKeyboardFocus -= HandleKeyboardFocus;
        AssociatedObject.GotMouseCapture -= HandleMouseCapture;
    }

    private static void HandleKeyboardFocus(object sender,
        System.Windows.Input.KeyboardFocusChangedEventArgs e)
    {
        var txt = e.NewFocus as TextBox;
        if (txt != null)
            txt.SelectAll();
    }

    private static void HandleMouseCapture(object sender,
        System.Windows.Input.MouseEventArgs e)
    {
        var txt = e.OriginalSource as TextBox;
        if (txt != null)
            txt.SelectAll();
    }
}
Run Code Online (Sandbox Code Playgroud)


Dut*_*tts 17

虽然这是一个老问题,但我刚刚遇到了这个问题但是使用附加行为来解决它,而不是谢尔盖的答案中的表达行为.这意味着我不需要System.Windows.Interactivity在Blend SDK中依赖:

public class TextBoxBehavior
{
    public static bool GetSelectAllTextOnFocus(TextBox textBox)
    {
        return (bool)textBox.GetValue(SelectAllTextOnFocusProperty);
    }

    public static void SetSelectAllTextOnFocus(TextBox textBox, bool value)
    {
        textBox.SetValue(SelectAllTextOnFocusProperty, value);
    }

    public static readonly DependencyProperty SelectAllTextOnFocusProperty =
        DependencyProperty.RegisterAttached(
            "SelectAllTextOnFocus",
            typeof (bool),
            typeof (TextBoxBehavior),
            new UIPropertyMetadata(false, OnSelectAllTextOnFocusChanged));

    private static void OnSelectAllTextOnFocusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var textBox = d as TextBox;
        if (textBox == null) return;

        if (e.NewValue is bool == false) return;

        if ((bool) e.NewValue)
        {
            textBox.GotFocus += SelectAll;
            textBox.PreviewMouseDown += IgnoreMouseButton;
        }
        else
        {
            textBox.GotFocus -= SelectAll;
            textBox.PreviewMouseDown -= IgnoreMouseButton;
        }
    }

    private static void SelectAll(object sender, RoutedEventArgs e)
    {
        var textBox = e.OriginalSource as TextBox;
        if (textBox == null) return;
        textBox.SelectAll();
    }

    private static void IgnoreMouseButton(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        var textBox = sender as TextBox;
        if (textBox == null || (!textBox.IsReadOnly && textBox.IsKeyboardFocusWithin)) return;

        e.Handled = true;
        textBox.Focus();
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,您可以在XAML中使用它,如下所示:

<TextBox Text="Some Text" behaviors:TextBoxBehavior.SelectAllTextOnFocus="True"/>
Run Code Online (Sandbox Code Playgroud)

我在这里写博客.


小智 15

这是MSDN上一个非常好的非常简单的解决方案:

<TextBox
    MouseDoubleClick="SelectAddress"
    GotKeyboardFocus="SelectAddress"
    PreviewMouseLeftButtonDown="SelectivelyIgnoreMouseButton" />
Run Code Online (Sandbox Code Playgroud)

这是背后的代码:

private void SelectAddress(object sender, RoutedEventArgs e)
{
    TextBox tb = (sender as TextBox);
    if (tb != null)
    {
        tb.SelectAll();
    }
}

private void SelectivelyIgnoreMouseButton(object sender,
    MouseButtonEventArgs e)
{
    TextBox tb = (sender as TextBox);
    if (tb != null)
    {
        if (!tb.IsKeyboardFocusWithin)
        {
            e.Handled = true;
            tb.Focus();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Sam*_*Sam 10

我认为这很有效:

private void ValueText_GotFocus(object sender, RoutedEventArgs e)
    {
        TextBox tb = (TextBox)e.OriginalSource;
        tb.Dispatcher.BeginInvoke(
            new Action(delegate
                {
                    tb.SelectAll();
                }), System.Windows.Threading.DispatcherPriority.Input);
    }
Run Code Online (Sandbox Code Playgroud)

如果您想将其实现为扩展方法:

public static void SelectAllText(this System.Windows.Controls.TextBox tb)
    {
        tb.Dispatcher.BeginInvoke(
            new Action(delegate
            {
                tb.SelectAll();
            }), System.Windows.Threading.DispatcherPriority.Input);
    }
Run Code Online (Sandbox Code Playgroud)

在你的GotFocus活动中:

private void ValueText_GotFocus(object sender, RoutedEventArgs e)
    {
        TextBox tb = (TextBox)e.OriginalSource;
        tb.SelectAllText();
    }
Run Code Online (Sandbox Code Playgroud)

我发现了上面的解决方案,因为几个月前我一直在寻找一种方法来将焦点设置为给定的UIElement.我在某处发现了下面的代码(特此授予)并且效果很好.我发布它即使它与OP的问题没有直接关系,因为它演示了使用Dispatcher处理UIElement的相同模式.

// Sets focus to uiElement
    public static void DelayedFocus(this UIElement uiElement)
    {
        uiElement.Dispatcher.BeginInvoke(
        new Action(delegate
        {
            uiElement.Focusable = true;
            uiElement.Focus();
            Keyboard.Focus(uiElement);
        }),
        DispatcherPriority.Render);
    }
Run Code Online (Sandbox Code Playgroud)

  • 我会避免使用这种方法,因为它依赖于在文本框的 MouseUp 处理程序之后运行的异步调用。我不相信这是 100% 确定的,并且可能会导致不一致的行为。尽管它不太可能发生,但我宁愿采用上述万无一失的方法。 (2认同)

Eli*_*ron 7

这里尝试用其他解决方案解决一些问题:

  1. 使用右键单击上下文菜单进行剪切/复制/过去选择所有文本,即使您没有选择所有文本。
  2. 从右键单击上下文菜单返回时,始终选择所有文本。
  3. 使用Alt+返回应用程序时Tab,始终选择所有文本。
  4. 当尝试在第一次单击时仅选择部分文本时,始终选择所有文本(例如,与 Google chromes 地址栏不同)。

我写的代码是可配置的。您可以通过设置三个只读字段来选择全选行为应该发生的操作:SelectOnKeybourdFocusSelectOnMouseLeftClickSelectOnMouseRightClick

此解决方案的缺点是它更复杂并且存储的是静态状态。它似乎与TextBox控件的默认行为进行了一场丑陋的斗争。尽管如此,它仍然有效,并且所有代码都隐藏在 Attached Property 容器类中。

public static class TextBoxExtensions
{
    // Configuration fields to choose on what actions the select all behavior should occur.
    static readonly bool SelectOnKeybourdFocus = true;
    static readonly bool SelectOnMouseLeftClick = true;
    static readonly bool SelectOnMouseRightClick = true;

    // Remembers a right click context menu that is opened 
    static ContextMenu ContextMenu = null;

    // Remembers if the first action on the TextBox is mouse down 
    static bool FirstActionIsMouseDown = false;

    public static readonly DependencyProperty SelectOnFocusProperty =
        DependencyProperty.RegisterAttached("SelectOnFocus", typeof(bool), typeof(TextBoxExtensions), new PropertyMetadata(false, new PropertyChangedCallback(OnSelectOnFocusChanged)));

    [AttachedPropertyBrowsableForChildren(IncludeDescendants = false)]
    [AttachedPropertyBrowsableForType(typeof(TextBox))]
    public static bool GetSelectOnFocus(DependencyObject obj)
    {
        return (bool)obj.GetValue(SelectOnFocusProperty);
    }

    public static void SetSelectOnFocus(DependencyObject obj, bool value)
    {
        obj.SetValue(SelectOnFocusProperty, value);
    }

    private static void OnSelectOnFocusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (!(d is TextBox textBox)) return;

        if (GetSelectOnFocus(textBox))
        {
            // Register events
            textBox.PreviewMouseDown += TextBox_PreviewMouseDown;
            textBox.PreviewMouseUp += TextBox_PreviewMouseUp;
            textBox.GotKeyboardFocus += TextBox_GotKeyboardFocus;
            textBox.LostKeyboardFocus += TextBox_LostKeyboardFocus;
        }
        else
        {
            // Unregister events
            textBox.PreviewMouseDown -= TextBox_PreviewMouseDown;
            textBox.PreviewMouseUp -= TextBox_PreviewMouseUp;
            textBox.GotKeyboardFocus -= TextBox_GotKeyboardFocus;
            textBox.LostKeyboardFocus -= TextBox_LostKeyboardFocus;
        }
    }

    private static void TextBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
    {
        if (!(sender is TextBox textBox)) return;

        // If mouse clicked and focus was not in text box, remember this is the first click.
        // This will enable to prevent select all when the text box gets the keyboard focus 
        // right after the mouse down event.
        if (!textBox.IsKeyboardFocusWithin)
        {
            FirstActionIsMouseDown = true;
        }
    }

    private static void TextBox_PreviewMouseUp(object sender, MouseButtonEventArgs e)
    {
        if (!(sender is TextBox textBox)) return;

        // Select all only if:
        // 1) SelectOnMouseLeftClick/SelectOnMouseRightClick is true and left/right button was clicked
        // 3) This is the first click
        // 4) No text is selected
        if (((SelectOnMouseLeftClick && e.ChangedButton == MouseButton.Left) || 
            (SelectOnMouseRightClick && e.ChangedButton == MouseButton.Right)) &&
            FirstActionIsMouseDown &&
            string.IsNullOrEmpty(textBox.SelectedText))
        {
            textBox.SelectAll();
        }

        // It is not the first click 
        FirstActionIsMouseDown = false;
    }

    private static void TextBox_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
    {
        if (!(sender is TextBox textBox)) return;

        // Select all only if:
        // 1) SelectOnKeybourdFocus is true
        // 2) Focus was not previously out of the application (e.OldFocus != null)
        // 3) The mouse was pressed down for the first after on the text box
        // 4) Focus was not previously in the context menu
        if (SelectOnKeybourdFocus &&
            e.OldFocus != null &&
            !FirstActionIsMouseDown &&
            !IsObjectInObjectTree(e.OldFocus as DependencyObject, ContextMenu))
        {
            textBox.SelectAll();
        }

        // Forget ContextMenu
        ContextMenu = null;
    }

    private static void TextBox_LostKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
    {
        if (!(sender is TextBox textBox)) return;

        // Remember ContextMenu (if opened)
        ContextMenu = e.NewFocus as ContextMenu;

        // Forget selection when focus is lost if:
        // 1) Focus is still in the application
        // 2) The context menu was not opened
        if (e.NewFocus != null
            && ContextMenu == null)
        {
            textBox.SelectionLength = 0;
        }
    }

    // Helper function to look if a DependencyObject is contained in the visual tree of another object
    private static bool IsObjectInObjectTree(DependencyObject searchInObject, DependencyObject compireToObject)
    {
        while (searchInObject != null && searchInObject != compireToObject)
        {
            searchInObject = VisualTreeHelper.GetParent(searchInObject);
        }

        return searchInObject != null;
    }
}
Run Code Online (Sandbox Code Playgroud)

要将附加属性附加到 a TextBox,您需要做的就是添加xmlns附加属性的 xml 命名空间 ( ),然后像这样使用它:

<TextBox attachedprop:TextBoxExtensions.SelectOnFocus="True"/>
Run Code Online (Sandbox Code Playgroud)

关于此解决方案的一些说明:

  1. 要覆盖鼠标按下事件的默认行为并在第一次单击时仅选择部分文本,请在鼠标按下事件时选择所有文本。
  2. 我不得不处理这样一个事实,即TextBox它在失去焦点后会记住它的选择。我实际上已经覆盖了这种行为。
  3. 我必须记住按下鼠标按钮是否是TextBoxFirstActionIsMouseDown静态字段)上的第一个操作。
  4. 我必须记住右键单击(ContextMenu静态字段)打开的上下文菜单。

我发现的唯一副作用是 whenSelectOnMouseRightClick是真的。有时右键单击上下文菜单在其打开并右键单击空白时会闪烁,而TextBox不是“全选”。


Dar*_*tel 6

在App.xaml文件中

<Application.Resources>
    <Style TargetType="TextBox">
        <EventSetter Event="GotKeyboardFocus" Handler="TextBox_GotKeyboardFocus"/>
    </Style>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)

在App.xaml.cs文件中

private void TextBox_GotKeyboardFocus(Object sender, KeyboardFocusChangedEventArgs e)
{
    ((TextBox)sender).SelectAll();
}
Run Code Online (Sandbox Code Playgroud)

使用此代码,您可以访问应用程序中的所有TextBox.


小智 5

我发现这里提到的答案都没有模仿标准的Windows文本框.例如,尝试单击文本框的最后一个字符和文本框右侧之间的空白区域.这里的大多数解决方案将始终选择整个内容,这使得将文本附加到文本框非常困难.

我在这里提出的答案在这方面表现得更好.这是一种行为(因此它需要Blend SDK中System.Windows.Interactivity程序集).它也可以使用附加属性重写.

public sealed class SelectAllTextOnFocusBehavior : Behavior<TextBox>
{
    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.PreviewMouseLeftButtonDown += AssociatedObject_PreviewMouseLeftButtonDown;
    }

    protected override void OnDetaching()
    {
        base.OnDetaching();
        AssociatedObject.PreviewMouseLeftButtonDown -= AssociatedObject_PreviewMouseLeftButtonDown;
    }

    void AssociatedObject_PreviewMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        // Find the textbox
        DependencyObject parent = e.OriginalSource as UIElement;
        while (parent != null && !(parent is TextBox))
            parent = VisualTreeHelper.GetParent(parent);

        var textBox = parent as TextBox;
        Debug.Assert(textBox != null);

        if (textBox.IsFocused) return;

        textBox.SelectAll();
        Keyboard.Focus(textBox);
        e.Handled = true;
    }
}
Run Code Online (Sandbox Code Playgroud)

这基于我在这里找到的代码.


Dan*_*ett 5

这个简单的实现非常适合我:

void TextBox_GotFocus(object sender, RoutedEventArgs e)
{
    ((TextBox) sender).SelectAll();
}

void TextBox_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    var TextBox = (TextBox) sender;
    if (!TextBox.IsKeyboardFocusWithin)
    {
        TextBox.Focus();
        e.Handled = true;
    }
}
Run Code Online (Sandbox Code Playgroud)

要将其应用于 all TextBox,请将以下代码放在InitializeComponent();

EventManager.RegisterClassHandler(typeof(TextBox), TextBox.GotFocusEvent, new RoutedEventHandler(TextBox_GotFocus));
EventManager.RegisterClassHandler(typeof(TextBox), TextBox.PreviewMouseDownEvent, new MouseButtonEventHandler(TextBox_PreviewMouseDown));
Run Code Online (Sandbox Code Playgroud)


baf*_*sar 5

我已经使用了尼尔斯的答案,但转换为更灵活。

public enum SelectAllMode
{

    /// <summary>
    ///  On first focus, it selects all then leave off textbox and doesn't check again
    /// </summary>
    OnFirstFocusThenLeaveOff = 0,

    /// <summary>
    ///  On first focus, it selects all then never selects
    /// </summary>
    OnFirstFocusThenNever = 1,

    /// <summary>
    /// Selects all on every focus
    /// </summary>
    OnEveryFocus = 2,

    /// <summary>
    /// Never selects text (WPF's default attitude)
    /// </summary>
    Never = 4,
}

public partial class TextBox : DependencyObject
{
    public static readonly DependencyProperty SelectAllModeProperty = DependencyProperty.RegisterAttached(
        "SelectAllMode",
        typeof(SelectAllMode?),
        typeof(TextBox),
        new PropertyMetadata(SelectAllModePropertyChanged));

    private static void SelectAllModePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        if (d is System.Windows.Controls.TextBox)
        {
            var textBox = d as System.Windows.Controls.TextBox;

            if (e.NewValue != null)
            {
                textBox.GotKeyboardFocus += OnKeyboardFocusSelectText;
                textBox.PreviewMouseLeftButtonDown += OnMouseLeftButtonDown;
            }
            else
            {
                textBox.GotKeyboardFocus -= OnKeyboardFocusSelectText;
                textBox.PreviewMouseLeftButtonDown -= OnMouseLeftButtonDown;
            }
        }
    }

    private static void OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
    {
        DependencyObject dependencyObject = GetParentFromVisualTree(e.OriginalSource);

        if (dependencyObject == null)
            return;

        var textBox = (System.Windows.Controls.TextBox)dependencyObject;
        if (!textBox.IsKeyboardFocusWithin)
        {
            textBox.Focus();
            e.Handled = true;
        }
    }

    private static DependencyObject GetParentFromVisualTree(object source)
    {
        DependencyObject parent = source as UIElement;
        while (parent != null && !(parent is System.Windows.Controls.TextBox))
        {
            parent = VisualTreeHelper.GetParent(parent);
        }

        return parent;
    }

    private static void OnKeyboardFocusSelectText(object sender, KeyboardFocusChangedEventArgs e)
    {
        var textBox = e.OriginalSource as System.Windows.Controls.TextBox;
        if (textBox == null) return;

        var selectAllMode = GetSelectAllMode(textBox);

        if (selectAllMode == SelectAllMode.Never)
        {
            textBox.SelectionStart = 0;
            textBox.SelectionLength = 0;
        }
        else
            textBox.SelectAll();

        if (selectAllMode == SelectAllMode.OnFirstFocusThenNever)
            SetSelectAllMode(textBox, SelectAllMode.Never);
        else if (selectAllMode == SelectAllMode.OnFirstFocusThenLeaveOff)
            SetSelectAllMode(textBox, null);
    }

    [AttachedPropertyBrowsableForChildrenAttribute(IncludeDescendants = false)]
    [AttachedPropertyBrowsableForType(typeof(System.Windows.Controls.TextBox))]
    public static SelectAllMode? GetSelectAllMode(DependencyObject @object)
    {
        return (SelectAllMode)@object.GetValue(SelectAllModeProperty);
    }

    public static void SetSelectAllMode(DependencyObject @object, SelectAllMode? value)
    {
        @object.SetValue(SelectAllModeProperty, value);
    }
}
Run Code Online (Sandbox Code Playgroud)

在 XAML 中,您可以使用以下之一:

<!-- On first focus, it selects all then leave off textbox and doesn't check again -->
<TextBox attprop:TextBox.SelectAllMode="OnFirstFocusThenLeaveOff" />

<!-- On first focus, it selects all then never selects -->
<TextBox attprop:TextBox.SelectAllMode="OnFirstFocusThenNever" />

<!-- Selects all on every focus -->
<TextBox attprop:TextBox.SelectAllMode="OnEveryFocus" />

<!-- Never selects text (WPF's default attitude) -->
<TextBox attprop:TextBox.SelectAllMode="Never" />
Run Code Online (Sandbox Code Playgroud)

  • 非常适合在模板中使用的解决方案,因为您可以将其绑定到 xaml,而无需任何实际的代码隐藏,只需扩展文本框的行为。 (2认同)