Copy and paste in Silverlight

10,747

Solution 1

I will assume that you mean TextBox rather than TextBlock as the latter doesn't provide cut and paste.

I think the easiest way to do this will be to derive from TextBox and override the key handling to prevent clipboard operation shortcuts. You would need to extend this basic solution if you wanted I18N support to allow for different shortcuts on different systems, but for the standard Ctrl+V, Ctrl+C (or Apple+C and Apple+V on Macs), this should work. You may also want to consider Ctrl+Ins as an alternative to Ctrl+V (I'm not certain Silverlight supports this, but it might).

Solution 2

Regarding the question above, the following article might help...

Custom Silverlight TextBox with contextmenu (Cut, Copy, Paste and Delete)

http://www.codeproject.com/KB/silverlight/SLCusTxtBox.aspx

Solution 3

The answer here is not the one you're going to want to hear but it's the most correct way IMHO.

TextBlock doesn't support copy/paste. Neither does the Label. I seem to remember this being down to the way that they get rendered to the screen.

The best way I'm aware of is to use a TextBox and style it so it looks like a TextBlock or a Label.

Here's a nice simple style for you to try out and modify - It should do the job okay.

<Style x:Key="ReadonlyTextBox" TargetType="TextBox">
    <Setter Property="BorderThickness" Value="1"/>
    <Setter Property="Background" Value="#FFFFFFFF"/>
    <Setter Property="Foreground" Value="#FF000000"/>
    <Setter Property="Padding" Value="2"/>
    <Setter Property="IsReadOnly" Value="True"/>
    <Setter Property="BorderBrush">
        <Setter.Value>
            <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                <GradientStop Color="#FFA3AEB9" Offset="0"/>
                <GradientStop Color="#FF8399A9" Offset="0.375"/>
                <GradientStop Color="#FF718597" Offset="0.375"/>
                <GradientStop Color="#FF617584" Offset="1"/>
            </LinearGradientBrush>
        </Setter.Value>
    </Setter>

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="TextBox">
                <Grid x:Name="RootElement">
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="CommonStates">
                            <VisualState x:Name="Normal"/>
                            <VisualState x:Name="MouseOver"/>
                            <VisualState x:Name="Disabled"/>
                            <VisualState x:Name="ReadOnly">
                                <Storyboard>
                                    <DoubleAnimation Duration="0" To="1" Storyboard.TargetProperty="Opacity" Storyboard.TargetName="ReadOnlyVisualElement"/>
                                    <ColorAnimation Duration="0" To="#00C9C9C9" Storyboard.TargetProperty="(Border.Background).(SolidColorBrush.Color)" Storyboard.TargetName="ReadOnlyVisualElement" d:IsOptimized="True"/>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="FocusStates">
                            <VisualState x:Name="Focused"/>
                            <VisualState x:Name="Unfocused"/>
                        </VisualStateGroup>
                        <VisualStateGroup x:Name="ValidationStates">
                            <VisualState x:Name="Valid"/>
                            <VisualState x:Name="InvalidUnfocused"/>
                            <VisualState x:Name="InvalidFocused"/>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <Border x:Name="Border" CornerRadius="1" Opacity="1">
                        <Grid>
                            <Border x:Name="ReadOnlyVisualElement" Background="#5EC9C9C9" Opacity="0"/>
                            <ScrollViewer x:Name="ContentElement" BorderThickness="0" IsTabStop="False" Padding="{TemplateBinding Padding}" Margin="1,1,-1,1"/>
                        </Grid>
                    </Border>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Now you've just got a bunch of Find & Replace to get busy with :-)

Share:
10,747
Admin
Author by

Admin

Updated on June 04, 2022

Comments

  • Admin
    Admin almost 2 years

    How can I disable the copy and paste functionality in a Silverlight textblock?