13

Possible Duplicate:
Any way to make a WPF textblock selectable?

Can I make a textblock selectable in WPF application so that a user can copy it.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
D J
  • 6,908
  • 13
  • 43
  • 75
  • Ah damn, Jay Riggs is right... I should have considered before re-iterating. +1 – Chris W. Oct 04 '12 at 04:17
  • 1
    The question can't be answered by erroneous answer with over 20 up-vote from provided link. I voted this question to reopen. It is no sense that it more one year old. – Hamlet Hakobyan Mar 04 '14 at 00:38
  • I found a better solution. Please check my answer here: https://stackoverflow.com/a/45627524/332528 – torvin Aug 11 '17 at 05:24

1 Answers1

14

You could just make it into a TextBox that's Read Only which just looks like a TextBlock, kind of like;

<Style x:Key="ReadOnlyTextBox" TargetType="TextBox">
   <Setter Property="IsReadOnly" Value="True" />
   <Setter Property="Padding" Value="5"/>
   <Setter Property="Margin" Value="0"/>
   <Setter Property="Background" Value="Transparent"/>
   <Setter Property="BorderBrush" Value="Transparent"/>
   <Setter Property="BorderThickness" Value="0"/>
   <Setter Property="IsTabStop" Value="False"/>
   <Setter Property="HorizontalScrollBarVisibility" Value="Disabled"/>
   <Setter Property="VerticalScrollBarVisibility" Value="Disabled"/>
   <Setter Property="Template">
      <Setter.Value>
         <ControlTemplate TargetType="TextBox">
            <Grid x:Name="RootElement">
               <ScrollViewer x:Name="ContentElement"
                             Margin="{TemplateBinding Margin}"
                             Background="{TemplateBinding Background}"
                             BorderBrush="{TemplateBinding BorderBrush}"
                             BorderThickness="{TemplateBinding BorderThickness}"
                             IsTabStop="{TemplateBinding IsTabStop}"
                             Padding="{TemplateBinding Padding}" 
                             HorizontalScrollBarVisibility="{TemplateBinding HorizontalScrollBarVisibility}"
                             VerticalScrollBarVisibility="{TemplateBinding VerticalScrollBarVisibility}"/>
            </Grid>
         </ControlTemplate>
      </Setter.Value>
   </Setter>
</Style>

The ScrollViewer ContentElement would be in a TextBox by default, you could substitute for a ContentPresenter instead if you like also.

Then put it into effect;

<TextBox Text="Blah Blah Blah you can copy me!" Style="{StaticResource ReadOnlyTextBox}"/>

Hope this helps!

ADDENDUM: As @doodleus pointed out in the comments. Template binding the Content Property within the template may be necessary. As "ContentElement" is a named part of the Silverlight TextBox control. One of the little nuance differences to watch for in the different xaml Variants. I must not have paid attention to the Tags when I originally created the example. So kudos to him for correcting me.

Chris W.
  • 22,835
  • 3
  • 60
  • 94
  • 2
    you can also Tab throw it in this case :) – eran otzap Oct 04 '12 at 08:44
  • 1
    one key bit that is missing is the following attribute in the scrollviewer: Content="{TemplateBinding Text}" . without this, the text in the textbox will not be visible. – doodlleus Jun 09 '16 at 09:01
  • 1
    or better yet, just do this: – doodlleus Jun 09 '16 at 09:20
  • @doodlleus test it as is. – Chris W. Jun 09 '16 at 12:16
  • 1
    I did, it needs the addition I gave – doodlleus Jun 17 '16 at 20:00
  • @doodlleus I apologize, you're absolutely correct. "ContentElement" is a named part specific to the Silverlight variation of the control making direct template binding not required. I must have skimmed right past the tags when I originally created the example years ago. Thanks and I've updated the answer accordingly to reflect your insight. Cheers – Chris W. Jun 19 '16 at 16:39