从 Excel 粘贴到 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?
}
}
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?
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
做起来并不容易
您应该使用
ClipboardHelper
解析剪贴板数据看看这个问题
It's not easy to do
You should parse clipboard data with
ClipboardHelper
take a look at this question