Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Copy and paste is a fundamental way for users to exchange data between apps or within an app. This article shows you how to implement copy and paste in WinUI and Universal Windows Platform (UWP) apps using the clipboard APIs. You'll learn how to copy, cut, and paste data, track clipboard changes, and use the DataPackage class to handle different data formats.
Note
You can also use these APIs in other desktop apps through Windows Runtime (WinRT) APIs. For more information, see Call Windows Runtime APIs in desktop apps.
For complete code examples that demonstrate different copy and paste scenarios, see the UWP clipboard sample on GitHub.
Check for built-in clipboard support
In many cases, you do not need to write code to support clipboard operations. Many of the default XAML controls you can use to create apps already support clipboard operations.
Get set up
First, include the Windows.ApplicationModel.DataTransfer namespace in your app. Then, add an instance of the DataPackage object. This object contains both the data the user wants to copy and any properties (such as a description) that you want to include.
Copy and cut
Copy and cut (also referred to as move) work almost exactly the same. Choose which operation you want by using the RequestedOperation property.
// copy
dataPackage.RequestedOperation = DataPackageOperation.Copy;
// or cut
dataPackage.RequestedOperation = DataPackageOperation.Move;
Set the copied content
Next, you can add the data that a user has selected to the DataPackage object. If this data is supported by the DataPackage class, you can use one of the corresponding methods of the DataPackage object. Here's how to add text by using the SetText method:
The last step is to add the DataPackage to the clipboard by calling the static SetContent method.
Paste
To get the contents of the clipboard, call the static GetContent method. This method returns a DataPackageView that contains the content. This object is almost identical to a DataPackage object, except that its contents are read-only. With that object, you can use either the AvailableFormats or the Contains method to identify what formats are available. Then, you can call the corresponding DataPackageView method to get the data.
async void OutputClipboardText()
{
DataPackageView dataPackageView = Clipboard.GetContent();
if (dataPackageView.Contains(StandardDataFormats.Text))
{
string text = await dataPackageView.GetTextAsync();
// To output the text from this example, you need a TextBlock control
TextOutput.Text = "Clipboard now contains: " + text;
}
}
Track changes to the clipboard
In addition to copy and paste commands, you may also want to track clipboard changes. Do this by handling the clipboard's ContentChanged event.
Clipboard.ContentChanged += async (s, e) =>
{
DataPackageView dataPackageView = Clipboard.GetContent();
if (dataPackageView.Contains(StandardDataFormats.Text))
{
string text = await dataPackageView.GetTextAsync();
// To output the text from this example, you need a TextBlock control
TextOutput.Text = "Clipboard now contains: " + text;
}
}
Related content
Windows developer