dotnet / wpf

WPF is a .NET Core UI framework for building Windows desktop applications.
MIT License
7.01k stars 1.16k forks source link

The TextBox's SelectionTextBrush property doesn't work #4571

Open alexdi220 opened 3 years ago

alexdi220 commented 3 years ago

P.S.: I've faced this behavior the research an issue of our VisualStudio wizard - the foreground of the selected text is wrong. It's reproduced only for VS2019. So it's confused me - the property doesn't work in a common case but affects foreground in VS. In addition, our wizard use .Net 4.7.2. Can't understand how it works. image

Upd.: I've turned on the high-contrast mode and the color of the selected text in VS wizard changed to Black because by default the SelectionTextBrush uses the system's HighlightTextColor. It looks like the VS team use some hack or know something about how to force working this property :)

//ReferenceSource
        private static Brush GetDefaultSelectionTextBrush()
        {
            Brush selectionTextBrush = new SolidColorBrush(SystemColors.HighlightTextColor);
            selectionTextBrush.Freeze();
            return selectionTextBrush;
        }

image

Actual behavior: The selected text still black.

image

Expected behavior: The selected text should be red.

Minimal repro:

    <Grid>
        <TextBox Text="Test" SelectionTextBrush="Red"/>
    </Grid>
miloush commented 3 years ago

There are two selection modes for text box based controls - the default one is using an adorner, i.e. a semitransparent rectangle is overlaid on top of the text.

According to https://github.com/microsoft/dotnet/blob/master/Documentation/compatibility/wpf-SelectionTextBrush-property-for-non-adorner-selection.md, the SelectionTextBrush property does nothing for adorner-based selection, although I think that should be mentioned in the property documentation.

In theory you should be able to disable adorner-based selection using AppContext flag, as described here: https://github.com/Microsoft/dotnet/blob/master/Documentation/compatibility/wpf-TextBox-PasswordBox-text-selection-does-not-follow-system-colors.md

That makes the property working for me in .NET FW, but I am not sure how to convince .NET Core to pick it up.

alexdi220 commented 3 years ago

@miloush Thanks. Your option working for me in 4.7.2. Looks like a document issue that should be described.

premshahdev commented 5 months ago

In dotnet 8,it is not working. Is there any Progress. ?

lindexi commented 4 months ago

@premshahdev I apologize for the delayed response. As @miloush mentioned, you simply need to toggle the switch. You can enable it by using the following code snippet in the App constructor of your WPF program within the .NET framework. You can find an example code project file on GitHub.

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

See https://stackoverflow.com/a/75660000

TheAjaykrishnanR commented 1 month ago

@premshahdev I apologize for the delayed response. As @miloush mentioned, you simply need to toggle the switch. You can enable it by using the following code snippet in the App constructor of your WPF program within the .NET framework. You can find an example code project file on GitHub.

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

See https://stackoverflow.com/a/75660000

How to make it work on RichTextBox ?