Reputation: 2491
I have encountered a problem while using the Clipboard in a WPF Application: My code looks like this:
var msg = "sample message for the clipboard";
Clipboard.Clear();
Clipboard.SetText(msg);
But only "\t\t\t\r\n" gets stored in my clipboard. This is the only code that uses the Clipboard in my application and it gets called.
*Edit: Found the error. I used the above code for a copy-paste operation in a DataGridRow. This works for that:
private void OnCopyingRowClipboardContent(object sender, DataGridRowClipboardEventArgs e)
{
var msg = "sample"
e.ClipboardRowContent.Clear();
e.ClipboardRowContent.Add(new DataGridClipboardCellContent(e.Item, (sender as DataGrid).Columns[0], msg));
}
I guess the problem was that it automatically tried to copy sth out of my DataGrid after my Clipboard.SetText(..) and overwrote my text again.
Upvotes: 7
Views: 10958
Reputation: 877
Clearing the Clipboard is redundant as SetText does that automatically for you.
This is what I usually use:
Clipboard.SetText(msg, TextDataFormat.Text);
or
Clipboard.SetText(msg,TextDataFormat.UnicodeText);
Reference is here
Upvotes: 7
Reputation: 4023
protected void clipboardSetText(string inTextToCopy)
{
var clipboardThread = new Thread(() => clipBoardThreadWorker(inTextToCopy));
clipboardThread.SetApartmentState(ApartmentState.STA);
clipboardThread.IsBackground = false;
clipboardThread.Start();
}
private void clipBoardThreadWorker(string inTextToCopy)
{
System.Windows.Clipboard.SetText(inTextToCopy);
}
Upvotes: 4