In WPF RadRichTextBox, we try to Insert AnnotationRanges using InsertFragment method. But, it throws exception in some scenarios when Track Changes is ON. Please find sample code below.
try
{
//Initialize
RadDocument associatedDocument = associatedRichTextBox.Document;
DocumentSelection selection = associatedDocument.Selection;
DocumentPosition documentPosition = associatedDocument.CaretPosition;
string selectedText = GetSelectedText(associatedDocument);
// If text is not selected
if (String.IsNullOrEmpty(selectedText) && String.IsNullOrWhiteSpace(selectedText))
{
MessageBox.Show("Please select a text", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
RadDocument tempDocument = associatedDocument.Selection.CreateDocumentFromSelection();
IEnumerable<FormulaRangeStart> formulaMarkers = tempDocument.GetAnnotationMarkersOfType<FormulaRangeStart>();
bool bformulaRangeExistsInSelection = false;
foreach (FormulaRangeStart frmla in formulaMarkers)
{
bformulaRangeExistsInSelection = true;
break;
}
if (bformulaRangeExistsInSelection)
{
MessageBox.Show("Selection already contains a formula range", "Formula Information", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
FormulaRangeStart formulaRangeStart = new FormulaRangeStart();
FormulaRangeEnd formulaRangeEnd = new FormulaRangeEnd();
formulaRangeEnd.PairWithStart(formulaRangeStart);
tempDocument.CaretPosition.MoveToFirstPositionInDocument();
tempDocument.InsertInline(formulaRangeStart);
tempDocument.CaretPosition.MoveToLastPositionInDocument();
tempDocument.InsertInline(formulaRangeEnd);
tempDocument.Selection.SelectAll();
IEnumerable<Paragraph> paragraphs = tempDocument.Selection.GetSelectedParagraphs();
foreach (Paragraph paragraph in paragraphs)
{
foreach (Telerik.Windows.Documents.Model.Inline inline in paragraph.Inlines)
{
if (inline is Span)
{
((Span)(inline)).HighlightColor = Colors.DarkGray;
}
}
}
tempDocument.CaretPosition.MoveToLastPositionInDocument();
Span spanResetBgColor = new Span(FormattingSymbolLayoutBox.SPACE_SYMBOL_VISUAL);
spanResetBgColor.HighlightColor = Colors.White;
tempDocument.InsertInline(spanResetBgColor);
tempDocument.Selection.SelectAll();
associatedRichTextBox.IsTrackChangesEnabled = true;
associatedRichTextBox.TrackChangesOptions.TrackFormatting = true;
associatedRichTextBox.InsertFragment(tempDocument.Selection.CopySelectedDocumentElements(true));
associatedRichTextBox.Insert(FormattingSymbolLayoutBox.ENTER);
associatedRichTextBox.Document.Selection.Clear();
associatedRichTextBox.UpdateLayout();
associatedRichTextBox.UpdateEditorLayout();
associatedRichTextBox.Focus();
if (associatedRichTextBox.ActiveEditorPresenter != null)
{
associatedRichTextBox.ActiveEditorPresenter.RecreateUI();
}
}
try
{
//Initialize
RadDocument associatedDocument = associatedRichTextBox.Document;
DocumentSelection selection = associatedDocument.Selection;
DocumentPosition documentPosition = associatedDocument.CaretPosition;
string selectedText = GetSelectedText(associatedDocument);
// If text is not selected
if (String.IsNullOrEmpty(selectedText) && String.IsNullOrWhiteSpace(selectedText))
{
MessageBox.Show("Please select a text", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
}
else
{
RadDocument tempDocument = associatedDocument.Selection.CreateDocumentFromSelection();
IEnumerable<FormulaRangeStart> formulaMarkers = tempDocument.GetAnnotationMarkersOfType<FormulaRangeStart>();
bool bformulaRangeExistsInSelection = false;
foreach (FormulaRangeStart frmla in formulaMarkers)
{
bformulaRangeExistsInSelection = true;
break;
}
if (bformulaRangeExistsInSelection)
{
MessageBox.Show("Selection already contains a formula range", "Formula Information", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
FormulaRangeStart formulaRangeStart = new FormulaRangeStart();
FormulaRangeEnd formulaRangeEnd = new FormulaRangeEnd();
formulaRangeEnd.PairWithStart(formulaRangeStart);
tempDocument.CaretPosition.MoveToFirstPositionInDocument();
tempDocument.InsertInline(formulaRangeStart);
tempDocument.CaretPosition.MoveToLastPositionInDocument();
tempDocument.InsertInline(formulaRangeEnd);
tempDocument.Selection.SelectAll();
IEnumerable<Paragraph> paragraphs = tempDocument.Selection.GetSelectedParagraphs();
foreach (Paragraph paragraph in paragraphs)
{
foreach (Telerik.Windows.Documents.Model.Inline inline in paragraph.Inlines)
{
if (inline is Span)
{
((Span)(inline)).HighlightColor = Colors.DarkGray;
}
}
}
tempDocument.CaretPosition.MoveToLastPositionInDocument();
Span spanResetBgColor = new Span(FormattingSymbolLayoutBox.SPACE_SYMBOL_VISUAL);
spanResetBgColor.HighlightColor = Colors.White;
tempDocument.InsertInline(spanResetBgColor);
tempDocument.Selection.SelectAll();
associatedRichTextBox.IsTrackChangesEnabled = true;
associatedRichTextBox.TrackChangesOptions.TrackFormatting = true;
associatedRichTextBox.InsertFragment(tempDocument.Selection.CopySelectedDocumentElements(true));
associatedRichTextBox.Insert(FormattingSymbolLayoutBox.ENTER);
associatedRichTextBox.Document.Selection.Clear();
associatedRichTextBox.UpdateLayout();
associatedRichTextBox.UpdateEditorLayout();
associatedRichTextBox.Focus();
if (associatedRichTextBox.ActiveEditorPresenter != null)
{
associatedRichTextBox.ActiveEditorPresenter.RecreateUI();
}
}