3

I have a form, which includes TextBlocks, Lables, Borders. And I want to be able to select text with the mouse like it would be some text in the table in MS Word or HTML table. And I can't use TextBox or RichTextBox instead. Is there a way to achive my goal?

<Grid Margin="20">
<Grid.RowDefinitions>
    <RowDefinition Height="auto"/>
    <RowDefinition Height="auto"/>
</Grid.RowDefinitions>
<Border BorderBrush="Black" BorderThickness="1">
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="some text in TextBlock" VerticalAlignment="Center"/>
        <Label Content="another text in Label"/>
    </StackPanel>
</Border>
<Border Grid.Row="1" BorderBrush="Black" BorderThickness="1">
    <StackPanel Orientation="Horizontal">
        <TextBlock Text="one more  in TextBlock" VerticalAlignment="Center"/>
        <Label Content="one more text in Label"/>
    </StackPanel>
</Border>

croxy
  • 4,082
  • 9
  • 28
  • 46
daewoosh
  • 193
  • 3
  • 11
  • 2
    Is there any particular reason why you can't use a styled TextBoxes instead? eg. https://stackoverflow.com/questions/136435/any-way-to-make-a-wpf-textblock-selectable or http://geekswithblogs.net/mhildreth/archive/2008/06/25/selectable-labels-in-wpf-to-allow-copy-and-paste.aspx – Arie Jan 11 '16 at 15:02
  • 2
    You can use a `TextBox` with `IsReadOnly ="True"` instead of `TextBlock` for this purpose – Salah Akbari Jan 11 '16 at 15:02
  • 2
    Was just about to ask the exact [same](http://stackoverflow.com/questions/12720096/how-can-i-make-textblock-as-selectable-so-that-user-can-copy-its-text/12720189#12720189) thing – Chris W. Jan 11 '16 at 15:02
  • 1
    Use a readonly textbox instead and render it like a label like [here](http://stackoverflow.com/a/2506504/4746087) – Kurt Van den Branden Jan 11 '16 at 15:23
  • Please clarify... do you want to select text across the textblock AND the label or within one control? – grek40 Jan 11 '16 at 16:42
  • @grek40, Well, may be I can use TextBoxes...But it allows me to select text across ONE control, but I need to select all the text or part it. Like it would be on a web page. On a web page I can select whatever I want however I want. – daewoosh Jan 12 '16 at 06:35

2 Answers2

3

Use TextBox instead of a TextBlock, like the following code:

<TextBox
     Background="Transparent"
     TextWrapping="Wrap"
     Text="{Binding Desired, Mode=OneWay}"
     IsReadOnly="True"
     BorderThickness="0"/>

To make it cleaner, careate a template for the TextBlock and use the previous TextBox inside it.

Mohammed A. Fadil
  • 9,205
  • 7
  • 50
  • 67
-2

You can achieve it like this:

<TextBlock Text="test2"
           FontSize="16"
           IsTextSelectionEnabled="true"
           SelectionHighlightColor="Green"
           x:Name="test"      
           Foreground="Black"
           TextWrapping="Wrap"
           TextAlignment="Left"/>

Just set IsTextSelectionEnabled to true. You can also chagne color of selected text by changing SelectionHighlightColor property.

Massimiliano Kraus
  • 3,638
  • 5
  • 27
  • 47
David Škarda
  • 40
  • 1
  • 5