I want to show a selection in a WPF TextBox even when it's not in focus. How can I do this?
5 Answers
I have used this solution for a RichTextBox, but I assume it will also work for a standard text box. Basically, you need to handle the LostFocus event and mark it as handled.
protected void MyTextBox_LostFocus(object sender, RoutedEventArgs e)
{
// When the RichTextBox loses focus the user can no longer see the selection.
// This is a hack to make the RichTextBox think it did not lose focus.
e.Handled = true;
}
The TextBox will not realize it lost the focus and will still show the highlighted selection.
I'm not using data binding in this case, so it may be possible that this will mess up the two way binding. You may have to force binding in your LostFocus event handler. Something like this:
Binding binding = BindingOperations.GetBinding(this, TextProperty);
if (binding.UpdateSourceTrigger == UpdateSourceTrigger.Default ||
binding.UpdateSourceTrigger == UpdateSourceTrigger.LostFocus)
{
BindingOperations.GetBindingExpression(this, TextProperty).UpdateSource();
}

- 12,076
- 4
- 31
- 44
-
+1 Nice solution - helped me with creating a search feature... Thx – reSPAWNed Sep 21 '11 at 17:50
-
2This mostly works, but breaks scrolling on non-focused RichTextBoxes with selections in them -- the selection highlight doesn't move with the text. – Cameron Aug 08 '14 at 21:52
-
Better answer below – Casey Anderson May 30 '16 at 10:56
-
this won't work if you never go into the textbox. If the selection is set before presenting the textbox, or the selection is changed somewhere else in the program, what do you have to do to show the selection when the textbox is inactive? – DanW Jan 23 '17 at 16:23
-
In a multiline textbox this won't work, it won't scroll the selection with the text. – henon May 07 '23 at 15:48
Another option is to define a separate focus scope in XAML to maintain the selection in the first TextBox.
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBox Grid.Row="0" Text="Text that does not loose selection."/>
<StackPanel Grid.Row="1" FocusManager.IsFocusScope="True">
<TextBox Text="Some more text here." />
<Button Content="Run" />
<Button Content="Review" />
</StackPanel>
</Grid>

- 7,897
- 5
- 43
- 52
-
1There's a good visual demonstration of this at http://wpfhacks.blogspot.com/2009/06/correct-way-keep-selection-in-textbox.html – mtlynch Jul 28 '11 at 16:40
-
TextBoxBase.IsInactiveSelectionHighlightEnabled Property has available since .NET Framework 4.5
public bool IsInactiveSelectionHighlightEnabled { get; set; }
-
3Two notes: (1) the inactive highlight is applied when focus is lost, so if the field never had focus, it won't appear. Important when you're setting the selection programmatically. (2) The default color for `SystemColors.InactiveSelectionHighlightBrushKey` is a barely-noticeable dim grey, so changing that to something more colorful is recommended. – fadden Jun 16 '19 at 23:02
-
In .NET 4 you can use the SelectionBrush property of the textbox. When you set it to for instance Brushes.Yellow you get a selection that is still well visible as an inactive selection. – henon May 07 '23 at 16:10
I found that the suggestions listed (add a LostFocus handler, defining a FocusScope) to not work, but I did come across the code listed here: http://naracea.com/2011/06/26/selection-highlight-and-focus-on-wpf-textbox/, which creates a custom Adorner that highlights the text when not focused.

- 1,473
- 15
- 29
-
The link is broken; it is now at https://jiribrossmann.com/projects/naracea/2011/06/26/selection-highlight-and-focus-on-wpf-textbox/ . – Zev Spitz Jan 27 '19 at 06:03