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

bound XamlProvider not updated when adding Paragraph in code behind

4 Answers 125 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Christoph
Top achievements
Rank 1
Christoph asked on 24 Apr 2012, 02:12 PM
Hi

for storing RadDocument content in DB we use the Xaml format and get the Xaml
using the XamlDataProvider, binding its Xaml dependendency property
to a property of some custom class.

This works very well and reliably in virtually all situations and with
all kinds if RTF content.

Now I have a situation where I append a new paragraph to the richtextbox's
Document using

currentParagraph.Parent.Children.AddAfter(currentParagraph, newPargraph);


In this case both XamlDataProvider.Xaml doesn't change und doesn't update my bound
property and also the richtextbox's document isn't updated visually: the new paragraph is not displayed.
However when i check the xaml of richtextbox.Document using

xaml = new XamlFormatProvider().Export(richtextbox.Document);


the new Paragraph is there.

So there seems to be a discrepancy between the
"logical" content of the document and the one actually rendered.

How can that be?
Is my way of appending the paragraph not the recommend one?
Do i need some extra code for updating the document (kind of refresh or so?)?

Thanks
Chris


4 Answers, 1 is accepted

Sort by
0
Accepted
Boby
Telerik team
answered on 25 Apr 2012, 09:02 AM
Hi Christoph,
You can sync the document structure with the document presentation by calling RadRichTextBox.UpdateEditorLayout() method, for example just after the paragraph insertion.

Kind regards,
Boby
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Christoph
Top achievements
Rank 1
answered on 25 Apr 2012, 10:26 AM
Hi Bobby

Thank you.
That works very well.

One last question.
(how) can I get a reference to the RadRichTextBox
from a RadDocument?

Thanks again,
Chris



0
Accepted
Iva Toteva
Telerik team
answered on 27 Apr 2012, 11:42 AM
Hi Chris,

The connection between RadRichTextBox and RadDocument is in the other direction, i.e. RadRichTextBox knows about the existence of RadDocument, while the document can be used on its own and has no reference to the rich text box.

If you need to get a reference to the rich text box just to update the editor's layout, you can use another approach. The value of the Xaml property of XamlDataProvider is updated when the DocumentContentChanged event is fired. The event is not fired when you manipulate the document structure directly, i.e. by adding an Inline to the Inlines of a Paragraph or a Paragraph to the Blocks (Children) of a Section. Instead of doing so and invoking UpdateEdiorLayout method after you have added a new paragraph, you can add the paragraph in a way that will cause an immediate update in the content of the document.

In case you just want to add an empty paragraph in the document, navigate the caret to the place you want to add the paragraph and use:

document.InsertInline(new Span(FormattingSymbolLayoutBox.ENTER));

If you want to insert more content in the document (a paragraph with text or several paragraphs), you can create a whole new RadDocument, add a section, paragraphs and spans. Next, create a DocumentFragment out of it. Last, use the InsertFragment method of RadDocument to insert the newly created content. Here is an example:
RadDocument documentToBeInserted = new RadDocument();
Section section = new Section();
Paragraph paragraph = new Paragraph();
Span span = new Span("Text inserted using DocumentFragment!");
paragraph.Inlines.Add(span);
section.Blocks.Add(paragraph);
documentToBeInserted.Sections.Add(section);
documentToBeInserted.MeasureAndArrangeInDefaultSize();
document.InsertFragment(new DocumentFragment(documentToBeInserted));

If you encounter any difficulties using this approach, please let us know.

Greetings,
Iva Toteva
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

0
Christoph
Top achievements
Rank 1
answered on 15 May 2012, 03:25 PM
Hi Iva

thanks for your pointers and recommandations!
Really helped me out a lot.

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