Mike Taulty's Blog
Bits and Bytes from Microsoft UK
Silverlight 4 Rough Notes: Clipboard Access


Mike Taulty's Blog

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.

Posted Wed, Nov 18 2009 11:10 AM by mtaulty
Filed under: ,


tom wrote re: Silverlight 4 Rough Notes: Clipboard Access
on Thu, Nov 19 2009 12:42 AM

please give us support for pasting images from the clipboard!! please MS!

John DeHope wrote re: Silverlight 4 Rough Notes: Clipboard Access
on Fri, Nov 20 2009 6:39 AM

There's really just two things I've needed for LOB apps that silverlight didn't provide. One is printing, of course. Two is the ability to upload screenshots without having to save an image file. If my users could take a screenshot and dump that directly into silverlight... man that would revolutionize what I can do for them!

Anonymous wrote re: Silverlight 4 Rough Notes: Clipboard Access
on Fri, Nov 20 2009 9:03 AM

Please give us support for pasting images from the clipboard! This would make it so much easier to upload images.

Users could just paste images into a Web form and have them uploaded automatically.

image upload with copy and paste would be make a program much more user friendly. Currently, out of all the ria platforms, the only one that you can do this with is JavaFX by extending java components.

Miguel Madero wrote re: Silverlight 4 Rough Notes: Clipboard Access
on Sun, Nov 29 2009 4:49 PM

Well SL can be a drop target now, so you could just drop the image into your "uploader" control and then take it from there.

I know copy/paste might be nicer, but eit, it's a step forward and another want to you might need to support anyway.

That said. I wish MS did it as well for images. I have an scenario where I've a graph and need to put it on the clipboard. I.e. user clicks copy and then goes to Outlook and paste it to send an email