Justin R
Justin R

Reputation: 381

WPF TextBox - don't hide the selection

WPF's TextBox (System.Windows.Controls.TextBox) appears to highlight selected text only when it has the focus. I need to make a TextBox continue to show the selection when focus is lost.

In a standard Win32 EDIT control I could achieve this with ES_NOHIDESEL. How can I get the equivalent in WPF?

Upvotes: 0

Views: 1632

Answers (1)

Brian R. Bondy
Brian R. Bondy

Reputation: 347416

You can handle the LostFocus event and set the event arg to e.Handled = true. In this way the TextBox will not know that it lost focus and will keep your selection.

    private void myTextCtrl_LostFocus(object sender, RoutedEventArgs e)
    {
        e.Handled = true;
    }

This will give you a similar thing to what you are looking for, but unlike the Win32 way, it will still show your selection in the highlighted color instead of dark gray.

If you really want to go through the effort you could also write XAML for <TextBox.SelectionBrush>.

Another way is to use FocusManager, you can read about this method here.

Upvotes: 2

Related Questions