Change SelectionHighlight foreground of a TextBlock
Asked Answered
E

0

7

First of all, my case is in UWP apps.

TextBlock has a property named SelectionHighlightColor that allows you specify the concrete color of the Selection Area when you select a text. Just like this:

XAML:

<TextBlock Text="This is a sample text" IsTextSelectionEnabled="True" SelectionHighlightColor="#FFFFE751" Foreground="Black" />

That displays as:

enter image description here

In case you haven't notice, the foreground of selected text is always White even the background of selection area is of light color.

So is there any way to change the foreground of Selected text of TextBlock control?

Echinoderm answered 23/10, 2015 at 5:53 Comment(5)
Cant see how to do this as TextBlock doesnt have a Template and is a sealed class so cant be extended. It has a SelectedText property and SelectioChanged event but cant see any brushes to changeKalsomine
The TextBox class in Silverlight had a SelectionForeground property for this.. but the UWP TextBlock class does not seem to have...Bait
Furthermore neither the RichTextBlock nor the RichEditBox have a property for what you want achieve.. imho with TextBlock there is no way to do that...Bait
Yes, most possibly there is no way to do this with TextBlock. Maybe I need to code the custom control that implement this effect, but this is totally not worth it.Echinoderm
I guess with the new Composition APIs in version 1511 you could do the following (just a wild guess): - set highlight color to the inverted value of what you want - get textblock visual - enumerate children to find the hightlight box (I guess there should be one) - invert the box's colors w/ an effectCadena

© 2022 - 2024 — McMap. All rights reserved.