从Excel粘贴到WPF DataGrid [英] Paste from Excel to WPF DataGrid

查看:647
本文介绍了从Excel粘贴到WPF DataGrid的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个DataGrid(称为TheGrid),我想实现复制和粘贴功能。复制功能非常好,但我不知道如何实现粘贴。我只需要从剪贴板中获取数据并分析自己?



命令绑定:

 < Window.CommandBindings> 
< CommandBinding Command =复制Executed =CommandBinding_ExecutedCanExecute =CommandBinding_CanExecute/>
< CommandBinding Command =PasteExecuted =CommandBinding_ExecutedCanExecute =CommandBinding_CanExecute/>
< /Window.CommandBindings>

菜单项:

 < MenuItem Header ={x:静态文化:TextResource.CopyMenuItem}Command =复制/> 
< 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))
{
//这很好,哇,很容易!
ApplicationCommands.Copy.Execute(null,TheGrid);
}
else if(e.Command.Equals(ApplicationCommands.Paste))
{
//我在这里做什么?有没有一个简单的方法粘贴像有复制?
//或者我需要使用Clipboard.GetData抓取数据并自行解析?
}
}


解决方案

不容易做到
您应该使用剪贴板数据解析剪贴板数据

查看这个问题


I have a DataGrid (called TheGrid) that I would like to implement copy and paste functionality on. The copy functionality works great but I don't know how to implement paste. Do I just need to get the data from the clipboard and parse myself?

The command bindings:

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

The menu items:

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

The code behind for 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?
    }
}

解决方案

It's not easy to do
You should parse clipboard data with ClipboardHelper
take a look at this question

这篇关于从Excel粘贴到WPF DataGrid的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆