Page Index Toggle Pages: 1 Send TopicPrint
Normal Topic Copy content (Read 4249 times)
sm.eagle
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 4
Joined: Aug 2nd, 2016
Copy content
Aug 2nd, 2016 at 8:53am
Print Post  
Hi all,

I'm testing your spreadsheet component for WPF and having some difficulties copying cell data with formatting. Using CTRL+C then CTRL+V appears to only copy the cell data. Any hits are appreciated.

Sam
  
Back to top
 
IP Logged
 
Meppy
God Member
*****
Offline


MindFusion support

Posts: 1783
Joined: Jul 20th, 2005
Re: Copy content
Reply #1 - Aug 2nd, 2016 at 11:57am
Print Post  
Hi,

You can use the following methods to copy and paste text data and formatting:

Code
Select All
void Copy()
{
	var activeSheet = workbookView.ActiveWorksheet;
	var selectionRange = workbookView.Selection.Range;
	var dataObject = new DataObject();

	// Copy data
	var cellData = activeSheet.CopyData(selectionRange);
	dataObject.SetData(cellData);

	// Copy styles
	var styleData = activeSheet.CopyStyles(selectionRange);
	dataObject.SetData(styleData);

	Clipboard.SetDataObject(dataObject, true);
}

void Paste()
{
	var activeSheet = workbookView.ActiveWorksheet;
	var dataObject = Clipboard.GetDataObject();

	// Paste data
	if (dataObject.GetDataPresent(typeof(CellData)))
	{
		var cellData = (CellData)dataObject.GetData(typeof(CellData));
		activeSheet.PasteData(workbookView.ActiveCell, cellData);
	}

	// Paste styles
	if (dataObject.GetDataPresent(typeof(StyleData)))
	{
		var styleData = (StyleData)dataObject.GetData(typeof(StyleData));
		activeSheet.PasteStyles(workbookView.ActiveCell, styleData);
	}
} 


Let me know if this helps.

Regards,
Meppy
  
Back to top
 
IP Logged
 
sm.eagle
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 4
Joined: Aug 2nd, 2016
Re: Copy content
Reply #2 - Aug 2nd, 2016 at 1:43pm
Print Post  
This works nicely, but can it be called in response to the user pressing Ctrl+C and Ctrl+V?
  
Back to top
 
IP Logged
 
Meppy
God Member
*****
Offline


MindFusion support

Posts: 1783
Joined: Jul 20th, 2005
Re: Copy content
Reply #3 - Aug 2nd, 2016 at 2:16pm
Print Post  
Hi,

You can remove the default Copy and Paste bindings from the WorkbookView:

Code
Select All
var copyBinding = workbookView.CommandBindings.Cast<CommandBinding>().Where(b => b.Command == ApplicationCommands.Copy).FirstOrDefault();
if (copyBinding != null)
	workbookView.CommandBindings.Remove(copyBinding);
var pasteBinding = workbookView.CommandBindings.Cast<CommandBinding>().Where(b => b.Command == ApplicationCommands.Paste).FirstOrDefault();
if (pasteBinding != null)
	workbookView.CommandBindings.Remove(pasteBinding); 


Then replace them with your own:

Code
Select All
workbookView.CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy, OnCopy, OnCanCopy));
workbookView.CommandBindings.Add(new CommandBinding(ApplicationCommands.Paste, OnPaste, OnCanPaste)); 


In the OnCopy and OnPaste event handlers simply call the Copy/Paste methods above:

Code
Select All
private void OnCanCopy(object sender, CanExecuteRoutedEventArgs e)
{
	var activeSheet = workbookView.ActiveWorksheet;
	e.CanExecute = activeSheet != null && !workbookView.Selection.IsEmpty;
}

private void OnCopy(object sender, ExecutedRoutedEventArgs e)
{
	Copy();
}

private void OnCanPaste(object sender, CanExecuteRoutedEventArgs e)
{
	e.CanExecute = true;
}

private void OnPaste(object sender, ExecutedRoutedEventArgs e)
{
	Paste();
} 


Regards,
Meppy
  
Back to top
 
IP Logged
 
sm.eagle
YaBB Newbies
*
Offline


I Love MindFusion!

Posts: 4
Joined: Aug 2nd, 2016
Re: Copy content
Reply #4 - Aug 3rd, 2016 at 6:10am
Print Post  
Thanks, this works.
  
Back to top
 
IP Logged
 
Page Index Toggle Pages: 1
Send TopicPrint