Paint text behind touch in RichTextBlock RRS feed

  • Question

  • This is very simple and I have many hours looking for it with no luck

    I want to paint (kind of background color) a text behind the user finger

    Is like when you select text but I don't want to only select the text, the really one thing to want to avoid is the default copy menu (not the context menu that appear when holding)

    You can disable the default stuff with IsTextSelectionEnabled = true and then play with PointerPressed, PointerMoved and PointerExited but just can't make it work

    At the same time I don't want to select on PointerPressed because it will fires when scroll, so I think on a Timer

    Any help will be appreciated

    This is on C#, VS 2013 and Windows Phone Store app

    Tuesday, December 9, 2014 6:26 AM

All replies

  • Hi SuNcO,

    From your description, I assume you want to select the text in RichTextBlock control when finger slips over it. Based on my test, I’ve tried to select text programmatically using PointerPressed and PointerReleased event. But I found if we set the IsTextSelectionEnabled to false, the control would not show the selection event I success in selecting the text in C#, the control didn’t show selection at last. I use the following code snippet to test.


    TextPointer start = rtb.ContentStart.GetPositionAtOffset(1, LogicalDirection.Forward);
                TextPointer end = rtb.ContentStart.GetPositionAtOffset(20, LogicalDirection.Forward);
                rtb.Select(start, end);

    We would keep the IsTextSelectEnabled to true, and using SelectionChanged event to highlight the selection to work around. Try the following code snippet.

    <RichTextBlock x:Name="rtb" SelectionChanged="rtb_SelectionChanged">
    private void rtb_SelectionChanged(object sender, RoutedEventArgs e)
                SolidColorBrush scb = new SolidColorBrush();
                scb.Color = Windows.UI.Colors.Red;
                rtb.SelectionHighlightColor = scb;

    Please feel free to let me know if you have any concerns.

    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. Click HERE to participate the survey.

    Wednesday, December 10, 2014 6:51 AM
  • That's exactly what I want to avoid. Maybe I need to explain a little more, sorry about my bad english.

    The intent app is an epub reader. I know is kind of zip file, with a content table, styles, xhtml's, etc.. All fine.

    When people read a book, they say "oh, this is interesting, I want to mark it" and people must be able to touch and "drag" finger to paint (not select) the text. As developer I must save the "coordinates" of that painted text to show it next time they open that ebook (modify the real file, because you maybe export it or something)

    Next (not in this question btw), if people touch that painted text, I must show a menu for change color, underline, etc..

    The perfect way must be a WebView, but as I read you can't detect many events, so I opt for a RichTextBlock (or a simple TextBlock) with tags such <Run>, <Underline>, <HyperLink>, images, FontFamily, etc.

    Also I read you can't paint background text sections, but you can make it work with some InlineUIContainer and a StackPanel and Background inside it.

    So I have many code, my next step is to figure out how people can apply background color to some touched text (or at least detect exact coordinates/TextPointer/PointerPoint/begin_end).

    Wednesday, December 10, 2014 6:42 PM
  • Hi!

    Did you find a solution? I'm in the exact same situation. I want to highlight a text in a richtextblock and I'd like to add a menu just above the selected text.


    Monday, February 29, 2016 10:12 AM