第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何自動選擇WPF TextBox中焦點上的所有文本?

如何自動選擇WPF TextBox中焦點上的所有文本?

智慧大石 2019-11-22 13:05:04
如果我SelectAll從GotFocus事件處理程序中調(diào)用,則無法使用鼠標-釋放鼠標后,選擇項就會消失。編輯:人們喜歡唐娜(Donnelle)的答案,我將盡力解釋為什么我不喜歡被接受的答案。它更復雜,而被接受的答案則以更簡單的方式完成相同的事情。接受答案的可用性更好。當您單擊文本的中間部分時,釋放鼠標時文本將不會被選中,從而使您可以立即開始編輯;如果仍然要選擇全部,只需再次按下該按鈕,這一次在釋放時不會取消選擇。按照Donelle的食譜,如果我單擊文本中間的內(nèi)容,則必須第二次單擊才能進行編輯。如果我單擊文本中的某個地方而不是文本外部的某個地方,這很可能意味著我想開始編輯而不是覆蓋所有內(nèi)容。
查看完整描述

3 回答

?
犯罪嫌疑人X

TA貢獻2080條經(jīng)驗 獲得超4個贊

不知道為什么它在GotFocus事件中丟失了選擇。

但是一種解決方案是對GotKeyboardFocus和GotMouseCapture事件進行選擇。這樣,它將始終有效。


查看完整回答
反對 回復 2019-11-22
?
互換的青春

TA貢獻1797條經(jīng)驗 獲得超6個贊

我們擁有它,因此,第一單擊將全選,然后單擊光標(我們的應用程序設計用于帶筆的數(shù)位板)。


您可能會發(fā)現(xiàn)它很有用。


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條經(jīng)驗 獲得超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 關(guān)注
  • 1259 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號