views:

3709

answers:

2

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"?

Thanks for any suggestions!

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

A: 

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}">
Jobi Joy
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.
Steve Cadwallader
+7  A: 

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/

Thanks for pointing that out, especially that the ControlTemplate is also ignored as I probably would have tried that failing all else.
Steve Cadwallader