When a table is copied (without a paragraph before it) and pasted as first element in a Section in RadRichTextBox with Track Changes enabled, accepting all changes throws (handled) InvalidCastException. Changes are not accepted.
Workaround: Attach to RadRichTextBox.CommandExecuting event, and add paragraph before the table in the document fragment in the clipboard.
private void RadRichTextBox_CommandExecuting(object sender, Telerik.Windows.Documents.RichTextBoxCommands.CommandExecutingEventArgs e)
{
if (e.Command is Telerik.Windows.Documents.RichTextBoxCommands.PasteCommand)
{
if (this.radRichTextBox.Document.IsTrackChangesEnabled)
{
DocumentFragment documentFromClipboard = ClipboardEx.GetDocument();
if (documentFromClipboard != null)
{
e.Cancel = true;
RadDocument documentFormClipboard = documentFromClipboard.ToDocument();
if (documentFormClipboard.EnumerateChildrenOfType<Table>().Count() > 0)
{
Section firstSection = documentFormClipboard.Sections.FirstOrDefault();
if (firstSection.EnumerateChildrenOfType<Table>().Count() > 0)
{
if (firstSection.EnumerateChildrenOfType<Table>().FirstOrDefault() != null)
{
Table table = firstSection.EnumerateChildrenOfType<Table>().FirstOrDefault();
Paragraph paragraph = new Paragraph() { SpacingAfter = 0, SpacingBefore = 0, LineSpacing = 0, FontSize = 1 };
firstSection.Blocks.AddBefore(table, paragraph);
RadDocumentEditor editor = new RadDocumentEditor(this.radRichTextBox.Document);
DocumentFragment fragment = new DocumentFragment(documentFormClipboard);
editor.InsertFragment(fragment);
}
}
}
}
}
}
}