6

WPF uses the system highlight color for painting the background of the selected text. I'd like to override it too.

I have a control template for textBox:

<ControlTemplate TargetType="TextBox">
    <Border Name="Border"
          CornerRadius="2" 
          Padding="2"
          Background="Transparent"
          BorderThickness="0" >
        <ScrollViewer Margin="0" x:Name="PART_ContentHost"/>
    </Border>
    <ControlTemplate.Triggers>
        <Trigger Property="IsEnabled" Value="False">
            <Setter TargetName="Border" Property="Background" Value="{StaticResource TextBoxDisabledBackgroundColor}"/>
            <Setter Property="Foreground" Value="{StaticResource TextBoxDisabledForegroundColor}"/>
        </Trigger>
        <Trigger Property="IsReadOnly" Value="false">
            <Setter TargetName="Border" Property="Background" Value="{StaticResource TextBoxBackgroundColor}"/>
            <Setter Property="Foreground" Value="Black"/>
        </Trigger>
    </ControlTemplate.Triggers>
</ControlTemplate>

How can I change this template to override the highlighted text and background color?

akjoshi
  • 15,374
  • 13
  • 103
  • 121
Diana
  • 229
  • 1
  • 5
  • 14

2 Answers2

8

In .NET 4 you can use the SelectionBrush property of the textbox.

Earlier versions require you to override system colours in-code, as there was no easily-exposed property for this - the textbox would just use the system-defined values.

Dan Puzey
  • 33,626
  • 4
  • 73
  • 96
-2

I did it with styles, e.g.:

  <Style x:Key="BoundedTextBox"  TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <DataTrigger Binding="{Binding IsAutoCalculated}" Value="True">
            <Setter Property="Background" Value="{StaticResource MyBlue}" />
        </Trigger>
    </Style.Triggers>
</Style>
Amittai Shapira
  • 3,749
  • 1
  • 30
  • 54