Silverlight 4 Rough Notes: Clipboard Access

Note – these posts are put together after a short time with Silverlight 4 as a way of providing pointers to some of the new features that Silverlight 4 has to offer. I’m posting these from the PDC as Silverlight 4 is announced for the first time so please bear that in mind when working through these posts.

I must use the clipboard hundreds of times a day. Business applications need clipboard access. Silverlight 3 only provided clipboard access in the form of a couple of built-in controls ( like TextBox ) providing copy/paste automatically like most TextBoxes in the world already do.

But in Silverlight 4 this is expanded to allow for more complete clipboard access.

Like many features in Silverlight, clipboard access must be in response to a user-initiated access ( i.e. you can’t write code on some background thread that tries to grab the clipboard – you must run this kind of code in response to a user action like a button click ).

Also, the formats for data on the clipboard are restricted. At the time of writing, the only format supported is text and so the clipboard class has 3 simple methods;

  • ContainsText
  • GetText
  • SetText

So, it’s beyond simple right now to interact with the Clipboard. If we have a UI such as;

<UserControl x:Class="SilverlightApplication9.MainPage"
    d:DesignHeight="300" d:DesignWidth="400">

    <Grid x:Name="LayoutRoot" Background="White">
            <RowDefinition />
            <RowDefinition />
            <RowDefinition />
            Click="Paste" />
            Text="{Binding Text}"
            Grid.Row="1" />
            Grid.Row="2" />

and a little bit of code running behind it such as;

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace SilverlightApplication9
  public partial class MainPage : UserControl
    public MainPage()

    private void Paste(object sender, RoutedEventArgs e)
      if (Clipboard.ContainsText())
        txtText.Text = Clipboard.GetText();

    private void Copy(object sender, RoutedEventArgs e)

then running that up gives me a UI like;


and clicking that Paste button puts up a consent UI;


If the user clicks No then the call to Clipboard.GetText() throws so that’s worth knowing about whereas if they click Yes then all’s well;


So – easy enough to have Clipboard access ( for text ) in a Silverlight 4 application. I’m hoping that one or two more formats might be enabled ( e.g. Images? ) as previews progress.