Search code examples
c#wpfrichtextbox

AppContext.SetSwitch("Switch.System.Windows.Controls.Text.UseAdornerForTextboxSelectionRendering", false); not work for RichTextBox?


When I set AppContext.SetSwitch("Switch.System.Windows.Controls.Text.UseAdornerForTextboxSelectionRendering", false); TextBox behaves normally, RichTextBox cannot display text. What is the reason for this result?

XAML:

<Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="*" />
            <RowDefinition Height="*" />
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*" />
            <ColumnDefinition Width="*" />
        </Grid.ColumnDefinitions>
        <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" Style="{StaticResource RichTextBoxStyle}"></RichTextBox>
       
    </Grid>

.Net6:

Codebedhind:

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

.NET Framework 4.8

App.config:

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

Is it possible to make the results of RichTextBox behave the same as the results of TextBox without changing the xaml code?

The result: enter image description here


Solution

  • What is the reason for this result?

    The fact that the switch only applies to TextBox and PasswordBox as stated in the docs.

    Is it possible to make the results of RichTextBox behave the same as the results of TextBox without changing the xaml code?

    At least not using this, or any other, switch. Set the SelectionOpacity programmatically if you don't want to touch the XAML markup? If you can call the AppContext.SetSwitch method in the code-behind, you might as well also set a property of a control.