Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select text in silverlight 3.0 text block

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?

like image 297
Gabriel McAdams Avatar asked Mar 12 '10 01:03

Gabriel McAdams


2 Answers

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.

like image 116
Gabriel McAdams Avatar answered Nov 07 '22 16:11

Gabriel McAdams


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.

like image 29
itowlson Avatar answered Nov 07 '22 17:11

itowlson