This is a migrated thread and some comments may be shown as answers.

Programatically modifying a RadDocument prior to assigning it to a RadRichTextBox

2 Answers 207 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Oliver
Top achievements
Rank 1
Oliver asked on 11 Jul 2011, 01:51 PM
For a proof of concept linked to my question described here (see my last post) I have begun experimenting with modifying a deserialized RadDocument prior to assigning it to the control. The idea behind this is to store links to external resources using the standard HyperlinkRangeStart-HyperLink-HyperlinkRangeStop concept and transforming those hyperlinks into custom controls hosted in InlineUIContainer objects.To test if this can work I've tried the following:

var doc = provider.Import(xml);

var hyperLinks = doc.EnumerateChildrenOfType<HyperlinkRangeStart>();
foreach (var link in hyperLinks)
{
var parent = link.Parent;
parent.Children.Remove(link);

InlineUIContainer container = new InlineUIContainer(new Button(), new Size(400, 300));
parent.Children.Add(container);
}

doc.History.Clear();

// BOOOM
doc.UpdateLayout();

This will crash in UpdateLayout or when I assign the document to a RadRichTextBox with the following exception:

InvalidOperationException "Stack Empty"

"   at System.Collections.Generic.Stack`1.Pop()\r\n   at Telerik.Windows.Documents.Layout.LayoutBox.UpdateFieldProperties(DocumentElement element, Stack`1 fieldStartsStack) in c:\\TB\\101\\WPF_Scrum\\Release_WPF_40\\Sources\\Development\\Documents\\Layout\\LayoutBox.cs:line 541\r\n   at Telerik.Windows.Documents.Layout.LayoutBox.UpdateFieldProperties(DocumentElement element, Stack`1 fieldStartsStack) in c:\\TB\\101\\WPF_Scrum\\Release_WPF_40\\Sources\\Development\\Documents\\Layout\\LayoutBox.cs:line 524\r\n   at Telerik.Windows.Documents.Layout.LayoutBox.UpdateFieldProperties(DocumentElement element, Stack`1 fieldStartsStack) in c:\\TB\\101\\WPF_Scrum\\Release_WPF_40\\Sources\\Development\\Documents\\Layout\\LayoutBox.cs:line 524\r\n   at Telerik.Windows.Documents.Layout.LayoutBox.UpdateFieldProperties(DocumentElement element) in c:\\TB\\101\\WPF_Scrum\\Release_WPF_40\\Sources\\Development\\Documents\\Layout\\LayoutBox.cs:line 520\r\n   at Telerik.Windows.Documents.Layout.DocumentLayoutBox.UpdateGlobalProperties() in c:\\TB\\101\\WPF_Scrum\\Release_WPF_40\\Sources\\Development\\Documents\\Layout\\DocumentLayoutBox.cs:line 276\r\n   at Telerik.Windows.Documents.Layout.DocumentLayoutBox.MeasureOverride(SizeF availableSize) in c:\\TB\\101\\WPF_Scrum\\Release_WPF_40\\Sources\\Development\\Documents\\Layout\\DocumentLayoutBox.cs:line 259\r\n   at Telerik.Windows.Documents.Layout.LayoutElement.MeasureCore(SizeF availableSize) in c:\\TB\\101\\WPF_Scrum\\Release_WPF_40\\Sources\\Development\\Documents\\Layout\\LayoutElement.cs:line 1473\r\n   at Telerik.Windows.Documents.Layout.LayoutElement.Measure(SizeF availableSize) in c:\\TB\\101\\WPF_Scrum\\Release_WPF_40\\Sources\\Development\\Documents\\Layout\\LayoutElement.cs:line 1352\r\n   at Telerik.Windows.Documents.Layout.ContextLayoutManager.UpdateLayout() in c:\\TB\\101\\WPF_Scrum\\Release_WPF_40\\Sources\\Development\\Documents\\Layout\\ContextLayoutManager.cs:line 360\r\n   at Telerik.Windows.Documents.Layout.LayoutElement.UpdateLayout() in c:\\TB\\101\\WPF_Scrum\\Release_WPF_40\\Sources\\Development\\Documents\\Layout\\LayoutElement.cs:line 982\r\n   at Telerik.Windows.Documents.Model.RadDocument.UpdateLayout() in c:\\TB\\101\\WPF_Scrum\\Release_WPF_40\\Sources\\Development\\Documents\\Model\\RadDocument.cs:line 706\r\n   at Mindspace.DocumentEditors.RichTextDocumentEditor.<SetEditorContent>b__3() in C:\\Users\\oliver\\Projects\\Mindspace\\SilverlightClient\\DocumentEditors\\RichTextDocumentEditor.cs:line 296"

2 Answers, 1 is accepted

Sort by
0
Oliver
Top achievements
Rank 1
answered on 11 Jul 2011, 08:53 PM
Would be nice if someone from Telerik staff could comment on this because it's quite urgent.
0
Iva Toteva
Telerik team
answered on 14 Jul 2011, 11:33 AM
Hi Oliver,

The source code in your snippet should be modified as follows:

var hyperLinks = document.EnumerateChildrenOfType<HyperlinkRangeStart>().Reverse();
foreach(HyperlinkRangeStart link inhyperLinks)
{
    document.CaretPosition.MoveToInline((InlineLayoutBox)link.FirstLayoutBox, 0);
    InlineUIContainer container = newInlineUIContainer(newButton(), newSize(400, 300));
    document.InsertInline(container);
    document.CaretPosition.Reset();
    link.End.Parent.Children.Remove(link.End);
    link.Parent.Children.Remove(link);
}
document.UpdateLayout();

In the code above we remove both HyperlinkRangeStart and HyperlinkRangeEnd, as they are pair. We also should add the InlineUIContainer at the position of the HyperlinkRangeStart.

Greetings,
Iva
the Telerik team

Register for the Q2 2011 What's New Webinar Week. Mark your calendar for the week starting July 18th and book your seat for a walk through of all the exciting stuff we will ship with the new release!

Tags
RichTextBox
Asked by
Oliver
Top achievements
Rank 1
Answers by
Oliver
Top achievements
Rank 1
Iva Toteva
Telerik team
Share this question
or