[英]Paste from Excel to WPF DataGrid
我有一個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?
}
}
做起來並不容易
您應該使用ClipboardHelper
解析剪貼板數據
看看這個問題
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.