2011-06-15 119 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