Layout of text in RichTextBox changes when table is inserted

2 posts, 0 answers
  1. Sodi We
    Sodi We avatar
    160 posts
    Member since:
    Apr 2010

    Posted 30 Mar 2011 Link to this post


    Hello,

    I'm using the RadRichTextBox throughout the application I'm developing. But I noticed something strange. When I run the application and insert a table in a RadRichTextBox under one or more lines of text, then the text is stretched (more space between lines and between words.)

    I'm using a TableSizePicker for inserting tables in the RadRichTextBox, like in the following code snippet. The TableSizePicker has a binding with the InsertTableCommand of the RadRichTextBox.
    <telerik:RadRibbonDropDownButton x:Name="btnTable" DataContext="{Binding Commands, ElementName=richTextEditor}" Command="{Binding Path=InsertTableCommand}" Style="{StaticResource ToolbarInsertTableDropDownButtonStyle}">
        <Image Source="..." Style="{StaticResource ToolbarButtonImageStyle}" ToolTipService.ToolTip="..."/>
        <telerik:RadRibbonDropDownButton.DropDownContent>
            <telerik:RadRibbonButton ClickMode="Press">
                <telerik:TableSizePicker DataContext="{Binding Commands, ElementName=richTextEditor}" Command="{Binding Path=InsertTableCommand}" />
            </telerik:RadRibbonButton>
        </telerik:RadRibbonDropDownButton.DropDownContent>
    </telerik:RadRibbonDropDownButton>

    The xaml code of the RadRichTextBox looks like this:
    <telerikDocuments:RadRichTextBox x:Name="richTextEditor" Background="{x:Null}" BorderBrush="{x:Null}"
                                     MinWidth="840" MaxWidth="920" FontFamily="Arial" FontSize="11"
                                     HorizontalAlignment="Left" 
                                     LostFocus="richTextEditor_LostFocus" Loaded="richTextEditor_Loaded"
                                     DocumentChanged="richTextEditor_DocumentChanged"
                                     DocumentChanging="richTextEditor_DocumentChanging">
       <telerikDocuments:RadRichTextBox.Document>
           <telerikDocumentsModel:RadDocument x:Name="rteDocument" 
                                                           LayoutMode="Flow"
                                                           SectionDefaultPageMargin="3, 3, 3, 3" 
                                                           PageViewMargin="3, 3"
                                                           ParagraphDefaultSpacingAfter="10" ParagraphDefaultSpacingBefore="0">
           </telerikDocumentsModel:RadDocument>
       </telerikDocuments:RadRichTextBox.Document>                
    </telerikDocuments:RadRichTextBox>

    I think that the issue is related to the code I use the set the default font and font size of a table when that table is inserted in the RadRichTextBox. Because the issue does not seem to occur when I remove this code:
    private void richTextEditor_DocumentChanged(object sender, EventArgs e)
    {
        if (this.richTextEditor != null && this.richTextEditor.Document != null)
        {
            this.richTextEditor.Document.DocumentElementAdded += Document_DocumentElementAdded;
        }
    }
    private void richTextEditor_DocumentChanging(object sender, EventArgs e)
    {
        if (this.richTextEditor != null && this.richTextEditor.Document != null)
        {
            this.richTextEditor.Document.DocumentElementAdded -= Document_DocumentElementAdded;
        }
    }
    private void Document_DocumentElementAdded(object sender, Telerik.Windows.Documents.Model.DocumentElementAddedEventArgs e)
    {
        if (e.DocumentElement is Table)
        {
            string font = "Arial";
            if (cmbFonts.SelectedItem != null)
                font = ((RadComboBoxItem)cmbFonts.SelectedItem).Content.ToString();
            e.DocumentElement.DefaultStyleSettings.SetPropertyValue(Span.FontFamilyProperty, new FontFamily(font));
            string fontSize = "11";
            if (cmbFontSizes.SelectedItem != null)
                fontSize = ((RadComboBoxItem)cmbFontSizes.SelectedItem).Content.ToString();
            e.DocumentElement.DefaultStyleSettings.SetPropertyValue(Paragraph.FontSizeProperty, fontSize);
            e.DocumentElement.DefaultStyleSettings.SetPropertyValue(Span.FontSizeProperty, fontSize);
        }
    }

    Any ideas?
    Thanks
    Sodi
  2. Iva Toteva
    Admin
    Iva Toteva avatar
    1319 posts

    Posted 05 Apr 2011 Link to this post

    Hi Sodi We,

    I tried your code, but for some reason I did not manage to reproduce the problem you are speaking about. It may be something with the layout mode or the panel the RichTextBox is in. Could you send over a sample project illustrating the behavior?
    Something I noticed about your code is that you are setting the FontSize property to a string value, but it is of type double. A handled exception would be thrown and the setting won't be applied.
    Looking forward to hearing from you again.

    Greetings,
    Iva
    the Telerik team
  3. DevCraft banner
Back to Top