#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);

            Clipboard.SetDataObject(data);
        }

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);
                MessageBox.Show(xaml);
            }
        }

728-001
728-002

Advertisement

#727 – Getting a List of Files from the Clipboard

In Windows, if you do a Copy operation on a set of files, a list of filenames will get added to the clipboard.  You can get access to this list of files using the Clipboard.GetFileDropList method.

    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = this;
        }

        public StringCollection FileList { get; set; }

        private void btnPasteFileList_Click(object sender, RoutedEventArgs e)
        {
            if (Clipboard.ContainsFileDropList())
            {
                FileList = Clipboard.GetFileDropList();
                RaisePropertyChanged("FileList");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string prop)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(prop));
        }
    }

The XAML for this sample includes a ListBox that just binds to the FileList property.

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <ListBox ItemsSource="{Binding FileList}"/>
        <Button Grid.Column="1" Content="Paste File List"
                VerticalAlignment="Top" HorizontalAlignment="Left"
                Padding="10,5" Margin="10"
                Click="btnPasteFileList_Click"/>
    </Grid>

727-001
727-002

#721 – A Tool for Viewing the Clipboard

Here’s a handy free tool for viewing the contents of the clipboard – Clipboard Master, by Jumping Bytes Software.

Once installed, you can press the Windows Key + V combination and a Clipboard Master window will pop up, showing you the full contents of the clipboard and allowing you to select an item to paste.  Notice that if there are multiple items on the clipboard, in different formats, you can choose which item to paste.

721-001

#720 – Discovering What Kind of Data Is on the Clipboard

You can use one of several static methods in the Clipboard class to find out what type of data is currently on the Windows clipboard.

There are several methods that check for a specific format (ContainsAudioContainsFileDropListContainsImage, and ContainsText).  You can also use the ContainsData method to check for a specific format.

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            StringBuilder sbCB = new StringBuilder();

            // Check for some specific formats
            if (Clipboard.ContainsAudio())
                sbCB.Append("Audio ");

            if (Clipboard.ContainsFileDropList())
                sbCB.Append("FileDropList ");

            if (Clipboard.ContainsImage())
                sbCB.Append("Image ");

            if (Clipboard.ContainsText())
                sbCB.Append("Text ");

            // Can also check for specific formats
            // (See System.Windows.DataFormats for full list)
            if (Clipboard.ContainsData(DataFormats.CommaSeparatedValue))
                sbCB.Append("CommaSeparatedValue ");

            if (Clipboard.ContainsData(DataFormats.EnhancedMetafile))
                sbCB.Append("EnhancedMetafile ");

            lblInfo.Content = "Clipboard contains: " + sbCB.ToString();
        }

720-001
720-002

#718 – Copying Text To and From the Clipboard

You can copy data to or from the Windows clipboard using static methods in the System.Windows.Clipboard class.

Here’s an example that copies text from a TextBox onto the clipboard and pastes text from the clipboard into a Label.

    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition/>
            <ColumnDefinition/>
        </Grid.ColumnDefinitions>

        <TextBox Name="txtFrom" Text="Twas the Night Before Christmas.." Margin="10"/>
        <Button Grid.Column="1" Content="Copy"
                HorizontalAlignment="Left" Padding="10,5" Margin="10"
                Click="btnCopy_Click"/>

        <Label Name="lblTo" Grid.Row="1" Content="Paste something here"
               Margin="10"/>
        <Button Grid.Row="1" Grid.Column="1" Content="Paste"
                VerticalAlignment="Top" HorizontalAlignment="Left"
                Padding="10,5" Margin="10"
                Click="btnPaste_Click"/>
    </Grid>

The Click handlers use the SetText and GetText methods of the Clipboard class to interact with the clipboard.  The SetText method copies data to the clipboard.  The GetText method pastes data from the clipboard.

        private void btnCopy_Click(object sender, RoutedEventArgs e)
        {
            Clipboard.SetText(txtFrom.Text);
        }

        private void btnPaste_Click(object sender, RoutedEventArgs e)
        {
            lblTo.Content = Clipboard.GetText();
        }

718-001

718-002

718-003

718-004

718-005