In this short article, we will explore how to access the clipboard using a Silverlight 4.0 application. Clipboard Access is one of the new features provided in Silverlight 4.0. The class ‘Clipboard’ with static methods is provided to interact with the clipboard local system. We will be using the following methods of Clipboard class:
- SetText: Set the Unicode data in the clipboard
- ContainsText: Asks the clipboard about the presence of the Unicode data
- GetText: Retrieves the Unicode data from the clipboard.
Step 1: Open VS2010 and create a Silverlight 4.0 Application. In the MainPage.xaml, write the following XAML code:
Step 2: In the click events of the ‘Copy’, ‘Cut’ and ‘Paste’ buttons, write the following code:
private void btnCopy_Click(object sender, RoutedEventArgs e)
private void btnCut_Click(object sender, RoutedEventArgs e)
txtData.SelectedText = "";
private void btnPaste_Click(object sender, RoutedEventArgs e)
txtData.SelectedText = Clipboard.GetText();
Step 3: Run the application and type some data in the textbox. Select some text from the textbox and click on the ‘Copy’ button. Silverlight will prompt asking for access rights for the clipboard as shown below:
Click on ‘Yes’ and then set your cursor in the textbox and click on the ‘Paste’ button. You will find the selected text pasted in the textbox. You can do the same thing for ‘Cut’ operation too.
If you are working on Silverlight 4, check some more Silverlight articles