wpf
wpf copied to clipboard
The TextBox's SelectionTextBrush property doesn't work
- .NET Core Version: Microsoft.NETCore.App 6.0.0-preview.3.21201.4 / .NET SDKs 6.0.100-preview.3.21202.5
- Windows version: 2004
- Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes
Problem description:
Hi, the SelectionTextBrush property of TextBox doesn't work. I've set it to Red but nothing happens.
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.

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;
}

Actual behavior: The selected text still black.

Expected behavior: The selected text should be red.
Minimal repro:
<Grid>
<TextBox Text="Test" SelectionTextBrush="Red"/>
</Grid>
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.
@miloush Thanks. Your option working for me in 4.7.2. Looks like a document issue that should be described.
In dotnet 8,it is not working. Is there any Progress. ?
@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
@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 ?