Silverlight – Implementing Clipboard support

Silverlight 4 (currently in beta) adds support for Clipboard! – However no option to access html in clipboard.
In Silverlight 3 we had copy/paste available in the Textbox, programmatic access could be done, some IE only solution exists via HTML DOM bridge (so not OOB) or cross-browser (involving Flash!). But now the game has changed as we now have an API for multi-platform Clipboard access.
In this beta, the support is for (Unicode) text-only, and the Clipboard class has 3 static methods:
• GetText()
• SetText()
• ContainsText()
Clipboard access can only be done from a user initiated action (mouse, keyboard), and user is prompted to acknowledge the first time Clipboard is set or read (once per session).
Silverlight 3 Clipboard Class

Below is the class for Clipboard support in silverlight 3 or 4 with an additional method GetHtmlData() to return formated text from clipboard.
Class Name : Clipboard.cs, Download Source Code.

public static class Clipboard
public static void Copy(KeyEventArgs e, string s);
private static string GetData(string type);
public static string GetHtmlData();
public static string GetTextData();
public static void SetData(string data);
public static bool IsEnabled;

Same tech. can be used in Silverlight 4 as this class provides methods like GetHtmlData() which can be used to get Html contents.This could be very helpfull in RichTextBox Editor,i.e if you could write parser for office html (mso) you may write code for copy/paste of contents from office applications to silverlight application.Example office excel cells will be treated as HTML table cells.Copying images from word documents will provide path to imagedata (VML) which can be then uploaded using OpenFileDialog calls..

Rajneesh Noonia

8 thoughts on “Silverlight – Implementing Clipboard support

  1. (ScriptObject)_jsPaste.InvokeSelf(new object[0]);
    _jsCopy.InvokeSelf(new object[] { data });
    always throws null i dono why


  2. This works on my local PC, however, upon deploying it on a dev server, it threw an exception.  I’m using GetHtmlData to get data from an excel worksheet.  Hope you can help me with this 🙂  Thanks!


  3. The thing is, it’s not even asking me for anything.  IE8 just closes and recovers the tab.  In Firefox, at least exception is being caught, but IE8 just closes.  I also tried to run IE as administrator but found no luck.  Any other suggestions?


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s