RadRichTextBox unuexpected behaviour

2 posts, 0 answers
  1. Greg
    Greg avatar
    34 posts
    Member since:
    Sep 2012

    Posted 29 Sep 2012 Link to this post

    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.
  2. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 02 Oct 2012 Link to this post

    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.

  3. UI for WPF is Visual Studio 2017 Ready
Back to Top