Skip to content

Latest commit

 

History

History
195 lines (147 loc) · 7.89 KB

File metadata and controls

195 lines (147 loc) · 7.89 KB
description Learn how to implement copy and paste functionality in WinUI and UWP apps using the clipboard APIs. Includes code examples and best practices.
title Copy and Paste in WinUI and UWP Apps
ms.date 10/15/2025
ms.topic how-to
keywords windows 11, uwp, winrt, windows runtime, winui
ms.localizationpriority medium

Copy and paste

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 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.

[!div class="nextstepaction"] Open the WinUI Gallery app and see Clipboard samples

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.

using Windows.ApplicationModel.DataTransfer;
...
var dataPackage = new DataPackage();
#include <winrt/Windows.ApplicationModel.DataTransfer.h>
using namespace winrt::Windows::ApplicationModel::DataTransfer;
...
DataPackage dataPackage;

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;
// 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:

dataPackage.SetText("Hello World!");
dataPackage.SetText(L"Hello World!");

The last step is to add the DataPackage to the clipboard by calling the static SetContent method.

Clipboard.SetContent(dataPackage);
Clipboard::SetContent(dataPackage);

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;
    }
}
void MainPage::OutputClipboardText()
{
    DataPackageView dataPackageView = Clipboard::GetContent();
    if (dataPackageView.Contains(StandardDataFormats::Text()))
    {
        hstring text = dataPackageView.GetTextAsync().get();
        // To output the text from this example, you need a TextBlock control
        TextOutput().Text(L"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;
    }
}
Clipboard::ContentChanged([this](auto const&, auto const&)
{
    DataPackageView dataPackageView = Clipboard::GetContent();
    if (dataPackageView.Contains(StandardDataFormats::Text()))
    {
        hstring text = dataPackageView.GetTextAsync().get();
        // To output the text from this example, you need a TextBlock control
        TextOutput().Text(L"Clipboard now contains: " + text);
    }
});

Related content