Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can TextBox selection be styled with a different font color?

Styling is a big feature of WPF. Surely it's possible to make a text box look exactly like the OS textbox?

I'm tired of the bleak desaturated color that the selection must have in order for the black text to be visible:

textbox selection comparison

Is this fixable?

like image 301
Roman Starkov Avatar asked Jan 10 '12 10:01

Roman Starkov


3 Answers

Unfortunately it might be impossible, the selection highlighting is done via an overlaying rectangle (brilliant idea, isn't it?), this makes it hard to even get the selection background-color you want while retaining text readablility. (Try setting SelectionOpacity to 1)

Also the selected text portion does not appear to be styled so changing the foreground colour of the selection is not easily possible either.

like image 174
H.B. Avatar answered Nov 01 '22 03:11

H.B.


In .NET 4.8 you can now set the AppContext flag UseAdornerForTextboxSelectionRendering to false

<configuration>
    <runtime>
        <AppContextSwitchOverrides value="Switch.System.Windows.Controls.Text.UseAdornerForTextboxSelectionRendering=false"/>
    </runtime>
</configuration>

And then use the new property SelectionTextBrush to achieve this like

<TextBox Text="Test WPF TextBox"
         SelectionBrush="#1174E6"
         SelectionTextBrush="White"/>

enter image description here

like image 24
Fredrik Hedblad Avatar answered Nov 01 '22 03:11

Fredrik Hedblad


Perhaps you need the SelectionBrush? It's a dependency property. (Not sure if it's available in .NET version < 4).

For finding what the current system colors are, you can use SystemColors class.

See an example here: WPF SystemColors: color of TextBox border.

like image 1
Vlad Avatar answered Nov 01 '22 02:11

Vlad