为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

智慧大石 2019-11-22 13:05:04
如果我SelectAll从GotFocus事件处理程序中调用,则无法使用鼠标-释放鼠标后,选择项就会消失。编辑:人们喜欢唐娜(Donnelle)的答案,我将尽力解释为什么我不喜欢被接受的答案。它更复杂,而被接受的答案则以更简单的方式完成相同的事情。接受答案的可用性更好。当您单击文本的中间部分时,释放鼠标时文本将不会被选中,从而使您可以立即开始编辑;如果仍然要选择全部,只需再次按下该按钮,这一次在释放时不会取消选择。按照Donelle的食谱,如果我单击文本中间的内容,则必须第二次单击才能进行编辑。如果我单击文本中的某个地方而不是文本外部的某个地方,这很可能意味着我想开始编辑而不是覆盖所有内容。
查看完整描述

3 回答

?
犯罪嫌疑人X

TA贡献2080条经验 获得超4个赞

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

但是一种解决方案是对GotKeyboardFocus和GotMouseCapture事件进行选择。这样,它将始终有效。


查看完整回答
反对 回复 2019-11-22
?
互换的青春

TA贡献1797条经验 获得超6个赞

我们拥有它,因此,第一单击将全选,然后单击光标(我们的应用程序设计用于带笔的数位板)。


您可能会发现它很有用。


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();

    }

}


查看完整回答
反对 回复 2019-11-22
?
元芳怎么了

TA贡献1798条经验 获得超7个赞

Donnelle的答案效果最好,但是必须派出一个新的班级来使用它很痛苦。


我没有在App.xaml.cs中为应用程序中的所有TextBox注册处理程序。这使我可以将Donnelle的答案与标准TextBox控件一起使用。


将以下方法添加到您的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();

    }

}

查看完整回答
反对 回复 2019-11-22
  • 3 回答
  • 0 关注
  • 1141 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信