#728 – Using the Clipboard to Transfer Other Types of Data

As with drag-and-drop, you can transfer data between two running WPF applications in a variety of formats, using the clipboard.

The full list of data formats that you can specify is listed as a set of static fields in the System.Windows.DataFormats class.  Keep in mind that these are just labels used by the two applications to communicate with each other what data format is being transfered.

Below is an example of transfering some Xaml data between two applications using the clipboard.

On the copy side:

        private void btnCopy_Click(object sender, RoutedEventArgs e)
            string xaml = XamlWriter.Save(e.Source);
            DataObject data = new DataObject(DataFormats.Xaml, xaml);


On the paste side:

        private void btnPaste_Click(object sender, RoutedEventArgs e)
            IDataObject data = Clipboard.GetDataObject();
            if (data.GetDataPresent(DataFormats.Xaml))
                string xaml = (string)data.GetData(DataFormats.Xaml);