从Excel复制粘贴到WPF数据表格

14

我有一个DataGrid(称为TheGrid),我想在其上实现复制和粘贴功能。 复制功能很好用,但我不知道如何实现粘贴。 我只需要从剪贴板获取数据并进行解析吗?

命令绑定:

<Window.CommandBindings>
    <CommandBinding Command="Copy" Executed="CommandBinding_Executed" CanExecute="CommandBinding_CanExecute" />
    <CommandBinding Command="Paste" Executed="CommandBinding_Executed" CanExecute="CommandBinding_CanExecute" />
</Window.CommandBindings>

菜单项:

<MenuItem Header="{x:Static culture:TextResource.CopyMenuItem}" Command="Copy"/>
<MenuItem Header="{x:Static culture:TextResource.PasteMenuItem}" Command="Paste"/>

CommandBinding_Executed的后台代码:

private void CommandBinding_Executed(object sender, ExecutedRoutedEventArgs e)
{
    if(e.Command.Equals(ApplicationCommands.Copy))
    {
        // This works great, wow that was easy!
        ApplicationCommands.Copy.Execute(null, TheGrid);
    }
    else if (e.Command.Equals(ApplicationCommands.Paste))
    {
        //What do I do here? Is there an easy way to paste like there was for copy?
        // Or do I need to grab data using Clipboard.GetData and parse it myself?
    }
}
1个回答

8

这并不容易
您需要使用ClipboardHelper解析剪贴板数据
可以参考这个问题


也许我错过了什么,但是ClipboardHelper在哪个命名空间中?我无法编译并且得到了漂亮的红色波浪线 :-/ - KrisTrip
请查看我回答中的链接。 - Navid Rahmani
3
啊,过去链接回答也足够好了。不过还是谢谢! - theMayer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接