2011-06-15 189 views
12

我有一个DataGrid(名为TheGrid),我想实现复制和粘贴功能。复制功能很好,但我不知道如何实现粘贴。我是否需要从剪贴板获取数据并解析自己?从Excel粘贴到WPF DataGrid

命令绑定:

<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? 
    } 
} 

回答

8

这并不容易做到
你应该分析剪贴板数据与ClipboardHelper
看看this question

+0

也许我错过了一些东西,但是什么命名空间是ClipboardHelper?我无法编译并正在获取perty红色的波形: -/ – KrisTrip 2011-06-16 18:25:15

+0

看看我的回答中的链接 – 2011-06-16 19:45:07

+0

oops,没有读得太远......谢谢:) – KrisTrip 2011-06-16 20:09:14