Search code examples
wpftextboxstyling

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?


Solution

  • 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