Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make TextBlock as selectable so that user can copy its text [duplicate]

Tags:

wpf

xaml

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.

Thanks in advance.

DJ

like image 961
D J Avatar asked Oct 04 '12 04:10

D J


People also ask

Is TextBlock editable?

TextBlock is not editable. Use TextBox instead. Save this answer.

How do I highlight a TextBox in WPF?

You can use the following code to achieve your purpose: textBoxToHighlight. Focus(); textBoxToHighlight. Select(0, textBoxToHighlight.

What is TextBlock?

Text block is the primary control for displaying read-only text in apps. You can use it to display single-line or multi-line text, inline hyperlinks, and text with formatting like bold, italic, or underlined.

What is TextBlock WPF?

The TextBlock control provides flexible text support for UI scenarios that do not require more than one paragraph of text. It supports a number of properties that enable precise control of presentation, such as FontFamily, FontSize, FontWeight, TextEffects, and TextWrapping.


1 Answers

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.

like image 199
Chris W. Avatar answered Sep 28 '22 10:09

Chris W.