How can you change the highlighted text color for a WPF TextBox?
Asked Answered
E

5

14

The WPF TextBox natively makes use of the System Highlight color for painting the background of selected text. I would like to override this and make it consistent since it varies by OS/user theme.

For ListBoxItems, there is a neat trick (see below) where you can override the resource key for the HighlightBrushKey to customize the System Highlight color in a focused setting:

<Style TargetType="ListBoxItem">
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}"
                         Color="LightGreen"/>
    </Style.Resources>
</Style>

The same trick does not work for the TextBox unfortunately. Does anyone have any other ideas, besides "override the ControlTemplate"?

NOTE: This behavior appears to be added to WPF 4.

Emeric answered 2/1, 2009 at 3:37 Comment(0)
P
10

As Steve mentioned : NOTE: This behavior appears to be added to WPF 4.

I bumped into the same problem.

As Dr.WPF says

"It is entirely impossible in the current .NET releases (3.0 & 3.5 beta). The control is hardcoded to use the system setting... it doesn't look at the control template at all."

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/bbffa6e3-2745-4e72-80d0-9cdedeb69f7f/

Pasturage answered 2/1, 2009 at 8:51 Comment(2)
Thanks for pointing that out, especially that the ControlTemplate is also ignored as I probably would have tried that failing all else.Emeric
First link in answer is dead - "404 | Page not found".Autoxidation
B
11

Since .NET 4, TextBoxBase.SelectionBrush

You can specify the brush that highlights selected text by setting the SelectionBrush and SelectionOpacity properties. The SelectionOpacity property specifies the opacity of the SelectionBrush.

eg.

<TextBox SelectionBrush="Red" SelectionOpacity="0.5"
         Foreground="Blue" CaretBrush="Blue">  
Bifrost answered 1/10, 2013 at 14:57 Comment(0)
P
10

As Steve mentioned : NOTE: This behavior appears to be added to WPF 4.

I bumped into the same problem.

As Dr.WPF says

"It is entirely impossible in the current .NET releases (3.0 & 3.5 beta). The control is hardcoded to use the system setting... it doesn't look at the control template at all."

http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/bbffa6e3-2745-4e72-80d0-9cdedeb69f7f/

Pasturage answered 2/1, 2009 at 8:51 Comment(2)
Thanks for pointing that out, especially that the ControlTemplate is also ignored as I probably would have tried that failing all else.Emeric
First link in answer is dead - "404 | Page not found".Autoxidation
H
0

This is a Windows 8.1 .Net 4.6.1 tested solution to customize the SelectionBrush of each TextBox in the app:

/// Constructor in App.xaml.cs
public App() : base()
{
    // Register an additional SelectionChanged handler for appwide each TextBox
    EventManager.RegisterClassHandler(typeof(TextBox), TextBox.SelectionChangedEvent, RoutedEventHandler(_textBox_selectionChanged));
}

private void _textBox_selectionChanged(object sender, RoutedEventArgs e)
{
    // Customize background color of selected text
    (sender as TextBox).SelectionBrush = Brushes.MediumOrchid;

    // Customize opacity of background color
    (sender as TextBox).SelectionOpacity = 0.5;
}

If you want to include RichTextBox replace type name TextBox 4 times by TextBoxBase.

Hakan answered 20/12, 2015 at 13:44 Comment(0)
S
-1

You can create a Style for the TextBox and write a Setter for the background. The TextBox style should be a default one so that any TextBox which comes under the visual tree will get the changed TextBox

<Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBox}">
Schrimsher answered 2/1, 2009 at 8:45 Comment(1)
That would change the background color of the entire text box, what I was looking for is how to change the background for the highlighted portion only. Thanks anyways though.Emeric
G
-1

Try this:

     <Trigger Property="IsHighlighted" Value="True">
                            <Setter TargetName="Border" Property="Background" Value="OrangeRed"/>
                            <Setter Property="Foreground" Value="White"/>
                        </Trigger>
Gossett answered 29/11, 2014 at 8:35 Comment(4)
IsHighlighted is not a property on TextBox, what element would you use that trigger on?Emeric
I looked again and confirmed, that is not a property on TextBox. msdn.microsoft.com/en-us/library/… I suspect you are using some other type of control, or a custom derived version of TextBox.Emeric
The example above is for a ListBox.Gossett
Ok, this question was about a TextBox. ListBox was given as an example where there was already a workaround available.Emeric

© 2022 - 2024 — McMap. All rights reserved.