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

RadRichTextBox unuexpected behaviour

1 Answer 92 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Greg
Top achievements
Rank 1
Greg asked on 29 Sep 2012, 07:12 AM
Hello,

I'm not sure if this is a bug or if it is intended to happen like this, but for some reason, if I try to add text to a RadRichTextBox after a window has finished loading, the rich text box behaves in an unexpected manner: it adds an extra span and it hides the text by default. It shows the text only after the text box is focused. This doesn't happen though if text is added to the rich text box before the window finished loading. You can easily see this by yourself.

So, on a new project, just add a rad rich text box and in the code-behind add:

public MainWindow()
{
InitializeComponent();
 
var section = new Section();
var paragraph = new Paragraph();
var span = new Span("Span declared in code-behind");
paragraph.Inlines.Add(span);
section.Blocks.Add(paragraph);
radRichTextBox.Document.Sections.Add(section);
}

That will work fine, but if you do this:

public MainWindow()
{
	InitializeComponent();
 
	Loaded += MainWindow_Loaded;
}
 
private void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
	var section = new Section();
	var paragraph = new Paragraph();
	var span = new Span("Span declared in code-behind");
	paragraph.Inlines.Add(span);
	section.Blocks.Add(paragraph);
	radRichTextBox.Document.Sections.Add(section);
}

you will see that the text box behaves completely different, and it is also prone to throwing different application errors in different circumstances.

1 Answer, 1 is accepted

Sort by
0
Iva Toteva
Telerik team
answered on 02 Oct 2012, 11:11 AM
Hello Greg,

The document has two states depending on if it has been measured or not. Some operations expect that the document has been measured, while others can only be performed on not measured documents. Manipulating the structure of the document explicitly (adding Inlines and Paragraphs to the Blocks and Inlines properties) can be performed on not measured documents only.

The document is measured by the framework when shown in the editor. After that, you should use the methods from RadRichTextBox's API - InsertTable, InsertInline, InsertBookmark, etc. when you need to manipulate the content of the document. 

This is also included in our online documentation in the articles RadDocument and Section.

To cut the long story short, you can either create a new document and set it to the Document property of the editor or create a DocumentFragment and insert it in the current document using the InsertFragment method. You can find examples of the use of document fragment in the Silverlight forum.

I hope this answers your question.

Kind regards,
Iva Toteva
the Telerik team

Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

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