开发者

Paste from Excel to WPF DataGrid

开发者 https://www.devze.com 2023-03-13 10:13 出处:网络
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 th

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"/>

T开发者_如何学Gohe 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

0

精彩评论

暂无评论...
验证码 换一张
取 消