How to switch order of selection rectangle and text for RichTextBox ('SelectionOpacity="1"', 'UseAdornerForTextboxSelectionRendering="false"')

WPF-Learner 106 Reputation points


I want to turn off the opacity of selection rectangle on text fields (such as: TextBox & RichTextBox), by setting SelectionOpacity="1". When doing so, the text becomes hidden, due to the default order of selection rectangle and the text itself. I've discovered this special flag: UseAdornerForTextboxSelectionRendering, that when set to false, switches the default order, and then the text becomes visible, even though the selection is set to opacity of 1.

This works wonderfully for System.Windows.Controls.TextBox, but unfortunately not for System.Windows.Controls.RichTextBox.

I've created a very small project that isolates the issue: Just create a new WPF project with the following code:


        <RowDefinition Height="*" />
        <RowDefinition Height="*" />
        <ColumnDefinition Width="*" />
        <ColumnDefinition Width="*" />
    <Label Content="TextBox" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="25"></Label>
    <TextBox Height="80" Width="500" Grid.Column="1" SelectionOpacity="1"></TextBox>

    <Label Content="RichTextBox" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center" FontSize="25"></Label>
    <RichTextBox Height="80" Width="500" Grid.Row="1" Grid.Column="1" SelectionOpacity="1"></RichTextBox>


public MainWindow()
    AppContext.SetSwitch("Switch.System.Windows.Controls.Text.UseAdornerForTextboxSelectionRendering", false);

This results in the following result for System.Windows.Controls.TextBox:

User's image

But unfortunately, it looks as following for System.Windows.Controls.RichTextBox:

User's image

How do I achieve the same result for RichTextBox, as the one I got for the regular TextBox?

Thank you for the help!

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,694 questions
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,481 questions
{count} votes

Accepted answer
  1. Hui Liu-MSFT 46,961 Reputation points Microsoft Vendor


    When I set AppContext.SetSwitch("Switch.System.Windows.Controls.Text.UseAdornerForTextboxSelectionRendering", false); the TextBox behaves fine and the RichTextBox cannot display the text.

    The fact that the switch only applies to TextBox and PasswordBox as stated in the docs. It doesn't work for RichTextBox.

    At least not using this, or any other, switch. It is recommended that you Set the SelectionOpacity programmatically if you don't want to change the XAML markup.

        <RichTextBox Name="richTextBox" Height="80" Grid.Row="1"  Width="500" Grid.Column="1" SelectionOpacity="1"></RichTextBox>


      richTextBox.SelectionOpacity = 0.7;

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

0 additional answers

Sort by: Most helpful