WPF 中 OpenClipboard 失败问题 - HRESULT: 0x800401D0 的解决方案

解决方案能够参照:https://stackoverflow.com/questions/12769264/openclipboard-failed-when-copy-pasting-data-from-wpf-datagridwindows

关键的问题在于 Clipboard.SetText() 方法会触发异常。this

 

方案一:spa

It is a bug in the WPF Clipboard handler. You need to handle the unhandled exception in the Application.DispatcherUnhandledException event.code

Add this attribute to the Application element in your App.xamlblog

DispatcherUnhandledException="Application_DispatcherUnhandledException"

 

Add this code to your App.xaml.cs fileip

void Application_DispatcherUnhandledException(object sender, DispatcherUnhandledExceptionEventArgs e)
{
    var comException = e.Exception as System.Runtime.InteropServices.COMException;

    if (comException != null && comException.ErrorCode == -2147221040)
         e.Handled = true;
}

 

方案二:element

We are using .NET 4.0. We had the same problem, but after logging off the system, code used to work fine for some time.get

Finally we found the alternative.string

If you want to copy a string to the clipboard,it

string data = "Copy This"

Till now I was using the following method

Clipboard.SetText(data);

It was failing again and again. Then I looked at other methods available to set text in the clipboard in Clipboard Class and tried the following:

Clipboard.SetDataObject(data);

And it worked :). I never had the issue again.

 

更多的方案参照最上方的连接。

 

若是是 DataGrid 的复制问题,通常 DataGridTextColumn 是能够直接进行复制的,若是是 DataGridTemplateColumn,则须要添加

ClipboardContentBinding="{Binding 属性}"

到 DataGridTemplateColumn 中就能解决此问题。

相关文章
相关标签/搜索