How to keep WPF TextBox selection when not focused?
Asked Answered
C

5

26

I want to show a selection in a WPF TextBox even when it's not in focus. How can I do this?

Conserve answered 13/3, 2009 at 12:43 Comment(0)
D
29

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();
     }
Downy answered 13/3, 2009 at 14:53 Comment(5)
+1 Nice solution - helped me with creating a search feature... ThxTergal
This mostly works, but breaks scrolling on non-focused RichTextBoxes with selections in them -- the selection highlight doesn't move with the text.Durant
Better answer belowEulogistic
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?Barometrograph
In a multiline textbox this won't work, it won't scroll the selection with the text.Lodi
D
13

TextBoxBase.IsInactiveSelectionHighlightEnabled Property has available since .NET Framework 4.5

public bool IsInactiveSelectionHighlightEnabled { get; set; }
Dorton answered 7/5, 2018 at 14:12 Comment(2)
Two 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.Gault
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.Lodi
G
10

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>
Gazette answered 21/8, 2010 at 16:10 Comment(2)
There's a good visual demonstration of this at wpfhacks.blogspot.com/2009/06/…Introjection
This solution is more to my convenience.Rolo
A
1
public class CustomRichTextBox : RichTextBox
{
     protected override void OnLostFocus(RoutedEventArgs e)
     {

     }
}
Alric answered 18/6, 2011 at 16:33 Comment(0)
W
1

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.

Wintergreen answered 21/5, 2012 at 14:26 Comment(1)
The link is broken; it is now at jiribrossmann.com/projects/naracea/2011/06/26/… .Accouterment

© 2022 - 2024 — McMap. All rights reserved.