using-wpf-clipboard

Implements WPF Clipboard copy and paste operations for text, images, files, and custom data formats.

Updated Nov 22, 2023
One-click install
npx skills add https://github.com/parksanghoon-sys/TestCode --skill using-wpf-clipboard-parksanghoon-sys
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: using-wpf-clipboard
Source: https://github.com/parksanghoon-sys/TestCode/tree/main/src/Modbus/wpf-dev-pack/.agents/skills/using-wpf-clipboard
Command: npx skills add https://github.com/parksanghoon-sys/TestCode --skill using-wpf-clipboard-parksanghoon-sys

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Implementing copy/paste functionality in WPF applications requires handling multiple data formats, clipboard locking errors, and inter-application data transfer, which involves repetitive boilerplate and platform-specific knowledge. ## Core Features & Use Cases - Multi-Format Clipboard Operations: Copy and paste text (Unicode, RTF, HTML), images (BitmapSource), file drop lists, and custom data formats using Clipboard and DataObject. - Clipboard Monitoring and Error Handling: Watch clipboard changes via AddClipboardFormatListener and handle ExternalException with retry logic when the clipboard is locked by another process. - MVVM Command Integration: Wire copy/paste logic into RelayCommand-based ViewModels with CanExecute checks against clipboard content. - Use Case: Build a WPF data grid tool where users copy selected items as both plain text and a custom serialized format, then paste them into another instance of the app or into Excel. ## Quick Start Ask the AI to implement copy and paste for text and images in your WPF application using the Clipboard class.

Frequently Asked Questions about using-wpf-clipboard

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I copy and paste text in WPF using Clipboard?▼

Use Clipboard.SetText to copy text and Clipboard.GetText to paste after checking Clipboard.ContainsText. You can specify formats like UnicodeText, Rtf, or Html via the TextDataFormat parameter.

How to copy an image to the clipboard in WPF?▼

Call Clipboard.SetImage with a BitmapSource, and retrieve it with Clipboard.GetImage after checking Clipboard.ContainsImage. To copy a UI element, render it into a RenderTargetBitmap first, then pass that bitmap to SetImage.

How do I transfer custom objects between WPF applications via clipboard?▼

Create a DataObject, register a custom format name with DataFormats.GetDataFormat, and call SetData with that format before Clipboard.SetDataObject. The receiving app checks GetDataPresent for the same format name and retrieves the object with GetData.

Why does Clipboard.SetText throw ExternalException in WPF?▼

The clipboard is a shared system resource and throws ExternalException when locked by another process. Wrap calls in try-catch and retry with a short delay, as shown in the TrySetTextWithRetry pattern.

How can I monitor clipboard changes in a WPF application?▼

Register the window handle with the Win32 AddClipboardFormatListener API and hook WndProc via HwndSource to receive WM_CLIPBOARDUPDATE messages. Remove the listener and hook when the window closes.

Does clipboard data persist after my WPF app closes?▼

Only if you call Clipboard.SetDataObject with the copy parameter set to true. With the default false value, the data is removed from the clipboard when the application exits.