13

Is it possible to allow a user to select text in a silverlight text block (not text box) as they would be able to in any HTML page?

Gabriel McAdams
  • 56,921
  • 12
  • 61
  • 77

2 Answers2

18

I later found a solution, and I wanted to share it. The solution can be found here.

Excerpt from that page:

...change the textbox's style. Put the following Xaml code in App.xaml or some other resource:

<Style x:Key="TextBoxStyle" TargetType="TextBox">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TextBox">
                <Grid x:Name="RootElement">       
                    <ScrollViewer x:Name="ContentElement" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" BorderThickness="0"/>       
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Then set your textbox's style as "{StaticResource TextBoxStyle}", and set IsReadOnly property as true, your textbox will look like a textblock but it can be copied.

Gabriel McAdams
  • 56,921
  • 12
  • 61
  • 77
  • Link doesn't work anymore, and this is a solution for `TextBox`. OP clearly says `TextBlock` – Julien Nov 29 '12 at 21:02
  • 3
    @Julien: I updated the link. If you look, I am the OP. I asked, and it was answered that there was no actual text block solution. It was a correct answer. That is why I accepted it. I found a work-around, and I posted it here. The work-around uses a textbox, styled to look and function like a text block (except that it allows the user to select and copy text). – Gabriel McAdams Nov 29 '12 at 22:23
8

No. The Silverlight TextBlock doesn't support selection. You would need to use a TextBox in read-only mode instead. To make the user experience a bit more seamless, you could style the TextBox to have a normal arrow cursor instead of an I-beam.

itowlson
  • 73,686
  • 17
  • 161
  • 157
  • That's what I keep reading. I was hoping that someone was able to do something better by using a behavior or something. – Gabriel McAdams Mar 12 '10 at 01:22
  • @Gabriel I had the same hopes, but unfortunately you can't. The TextBlock doesn't render text in the same way that a TextBox does. – Corey Sunwold Mar 12 '10 at 01:25