0

I have a problem with TextBlock text selection (Windows Store apps, C#).

If I set IsTextSelectionEnabled = True, then I can't Tap on the text. I need to be able to open popup on taping text and select this text, but it seems like I can't do both.

Is there a way to have both - Text Selection and Tap - for one TextBlock?

Edited: I need Text Selection to be able copy text.

Edited: TextSelection and Copy feature works for multiple word text, where not every word is tappable.

  1. Set TextBlock property IsTextSelectionEnabled = True

  2. Set text to TextBlock, using TextBlock.Inlines:

    textBlock.Inlines.Add(new Run() { Text = "Click " });
    Hyperlink hyperlink = new Hyperlink();
    hyperlink.Inlines.Add(new Run() { Text = "here" });
    hyperlink.Click += hyperlink_Click;
    textBlock.Inlines.Add(hyperlink);
    

But if whole text is tappable, this solution doesn't help.

  1. If I set text like this:

    textBlock.Text = "Click here";
    textBlock.Tapped += textBlock_Tapped;
    

Then I can't tap text. I can only copy this text.

  1. If I set text like this:

    Hyperlink hyperlink = new Hyperlink();
    hyperlink.Inlines.Add(new Run() { Text = "Click here" });
    hyperlink.Click += hyperlink_Click;
    textBlock.Inlines.Add(hyperlink);
    

Then I can't copy text. I can only tap on it.

1 Answers1

0

Using IsTextSelectionEnabled will handle Tap event by itself. So you will not have ability to use both. If you want to show PopUp when text get selected you can use SelectionChanged event.

UPDATE

If you just need user to be able to copy any part of text you don't need to create any PopUp. Just enable IsTextSelectionEnabled and user will be able to select any text part, press right mouse button and copy text using TextBlock's own PopUp. And in case it's touch or pen interaction this PopUp will be shown right after user selects text.

enter image description here

khamitimur
  • 1,078
  • 13
  • 22
  • If I use `SelectionChanged` event, then it will always open Popup on text selection. I need the ability to open PopUp, when user taps on the text, and the ability to **copy** the same text. Is this possible? – Victoria O. Mar 03 '16 at 12:02
  • @VictoriaO. so you want user to be able to choose any text or just word? – khamitimur Mar 03 '16 at 12:38
  • Any text. Sometimes there is only one word and it is tappable. – Victoria O. Mar 03 '16 at 12:54
  • @VictoriaO. so why don't you just use RichTextBlock? It'll provide user with ability to copy text by itself. After selecting text user will have ability to copy it by pressing right mouse button or if user uses pen or touch popup will be shonw right after text will be selected. – khamitimur Mar 03 '16 at 13:39
  • You didn't understand me correctly. The PopUp is not for copy. For example, I have a TextBlock with text "More info". When I tap on it, it displays me a PopUp with specific information, but I need an ability to copy text "More info". – Victoria O. Mar 03 '16 at 13:55