How to Copy Text to Clipboard in Windows Phone 8

Windows Phone by default has a builtin mechanism to support copy and paste functionality, but in some case we may need to handle the copy experience explicitly.

We won't be able to paste outside a TextBox, since this is handled by the Operating System functionality. So, there is no need to handle this in code.

The Windows Phone “System.Windows” namespace exposes the Clipboard class having the following three methods to handle the Clipboard. They are:

  • ContainsText()
  • GetText()
  • SetText(string)

ContainsText(): The ContainsText() method queries the clipboard for the presence of data in the Unicode text format and returns a bool value of true or false.

Code snippet

GetText(): The GetText() method helps to get the copied text from the clipboard. This method returns the copied Unicode text data if text is present in the clipboard, otherwise it returns an empty string.

Code snippet

Clipboard.GetText();

SetText(string text): The SetText(string text) method sets Unicode text to store/copy on the clipboard.

Code snippet

Clipboard.SetText("text to copy");

Now, here is the demo for Windows Phone 8.

Create a new Windows Phone 8 project and create one button and two textboxes as shown in the following clipboard operation.


Figure 1: PageName

XAML Code

  1. <phone:PhoneApplicationPage  
  2. x:Class="ClipBoardDemo.MainPage"  
  3.     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
  4.     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"  
  5.     xmlns:phone="clr-namespace:Microsoft.Phone.Controls;assembly=Microsoft.Phone"  
  6.     xmlns:shell="clr-namespace:Microsoft.Phone.Shell;assembly=Microsoft.Phone"  
  7.     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"  
  8.     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"  
  9. mc:Ignorable="d"  
  10. FontFamily="{StaticResource PhoneFontFamilyNormal}"  
  11. FontSize="{StaticResource PhoneFontSizeNormal}"  
  12. Foreground="{StaticResource PhoneForegroundBrush}"  
  13. SupportedOrientations="Portrait" Orientation="Portrait"  
  14. shell:SystemTray.IsVisible="True">  
  15.     <!--LayoutRoot is the root grid where all page content is placed-->  
  16.     <Grid x:Name="LayoutRoot" Background="Transparent">  
  17.         <Grid.RowDefinitions>  
  18.             <RowDefinition Height="Auto"/>  
  19.             <RowDefinition Height="*"/>  
  20.         </Grid.RowDefinitions>  
  21.         <!--TitlePanel contains the name of the application and page title-->  
  22.         <StackPanel x:Name="TitlePanel" Grid.Row="0" Margin="12,17,0,28">  
  23.             <TextBlock Text="MY APPLICATION" Style="{StaticResource PhoneTextNormalStyle}" Margin="12,0"/>  
  24.             <TextBlock Text="page name" Margin="9,-7,0,0" Style="{StaticResource PhoneTextTitle1Style}"/>  
  25.         </StackPanel>  
  26.         <!--ContentPanel - place additional content here-->  
  27.         <Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"></Grid>  
  28.         <Button Content="CopyToClipBoard" HorizontalAlignment="Left" Margin="107,229,0,0" Grid.Row="1" VerticalAlignment="Top" Width="260" Click="Button_Click_1"/>  
  29.         <TextBox HorizontalAlignment="Left" Height="72" TextWrapping="Wrap" VerticalAlignment="Top" Width="456" Margin="0,321,0,0" Grid.Row="1"/>  
  30.         <TextBox x:Name="clipboardTextbox" HorizontalAlignment="Left" Height="72" TextWrapping="Wrap" VerticalAlignment="Top" Width="456" Margin="2,152,0,0" Grid.Row="1"/>  
  31.     </Grid>  
  32. </phone:PhoneApplicationPage>
Now, write the following code for the button click event.

C# Code
  1. private void Button_Click_1(object sender, RoutedEventArgs e)   
  2. {  
  3.     Clipboard.SetText(clipboardTextbox.Text.ToString());  
  4. }
Next, run your application and enter some text into the clipboardTextbox as shown in the following screenshot.


Figure 2: Enter text in the ClipBoard TextBox

Click the CopyToClipBoard button to copy the text to the clipboard. Now, place the cursor onto another TextBox and you can see the paste symbol, press paste to paste the copied text as shown in the following screenshot.


Figure 3: Copy To ClipBoard

Up Next
    Ebook Download
    View all
    Learn
    View all