How to change highlighted text color for TextBox?

WPF uses the system's highlight color to paint the background of selected text. I would also like to redefine it.

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 to change this template to override the selected text and background color?

+2
source share
2 answers

In .NET 4, you can use the SelectionBrush property of a text field.

In earlier versions, you need to redefine the system colors in the code, because there was no easily accessible property for this - the text field would simply use the system values.

+5
source

I did this with styles, for example:

  <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> 
-1
source

Source: https://habr.com/ru/post/917194/


All Articles