Skip to main content

Open-WebUI-Functions is here! New Pipelines, Filters, and more. Learn more

WinUI Clipboard Integration

Gets and sets information from the clipboard object.

Learn more

Copy to Clipboard

In WinUI you can use the DataPackage class to put text on the clipboard. Here is an example in C#:

using Microsoft.UI.Xaml;
using Windows.ApplicationModel.DataTransfer;
private void CopyToClipboard(string text)
{
var dataPackage = new DataPackage();
dataPackage.SetText(text);
Clipboard.SetContent(dataPackage);
Clipboard.Flush();
}

You can then call this method and pass the desired text:

CopyToClipboard("This is the text to copy");

This code creates a DataPackage, puts the text on it and then puts it on the clipboard. Clipboard.Flush() ensures that the data is immediately available.

Paste from Clipboard

To get text from clipboard you can use DataPackageView class. Here is an example in C#:

using Microsoft.UI.Xaml;
using Windows.ApplicationModel.DataTransfer;
private string GetTextFromClipboard()
{
var dataPackageView = Clipboard.GetContent();
if (dataPackageView.Contains(StandardDataFormats.Text))
{
return dataPackageView.GetTextAsync().GetResults();
}
return string.Empty;
}

You can then call this method to get the text from the clipboard:

string clipboardText = GetTextFromClipboard();

This code gets the contents of the clipboard and checks if there is text. If so, the text is returned; otherwise an empty string is returned. Note that GetTextAsync().GetResults() is called synchronously as this is required within a synchronous context.

Best practices

  • Always call Clipboard.Flush() after setting content if you need the data to persist when the app is suspended.
  • Prefer async clipboard APIs (GetTextAsync) inside asynchronous flows to avoid UI thread stalls.
  • Guard clipboard reads with format checks (StandardDataFormats) so you only process supported payloads.

Handling other data formats

WinUI supports additional clipboard formats beyond text. For example, to work with images you can validate StandardDataFormats.Bitmap and retrieve the data through GetBitmapAsync. Structured data can be encoded using SetData with a custom format name so companion apps can deserialize it consistently.