我有一个 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?
}
}