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

Second InsertFragment Different From First

1 Answer 84 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Tyson
Top achievements
Rank 1
Tyson asked on 31 May 2012, 02:57 PM
Essentially I'm using the InsertFragment to insert some premade spans with annotations into the document. One of the requirements is to be able to insert the annotations around text, which is why I'm not just using the Insert function. Later on, I will also be inserting custom controls with Annotations around them. End goal is to use the document, saved in a database, as a template for displaying dynamic data.

The issue I'm running into is that when I use the InsertFragment function the first time, it works great. When I call it a second time somewhere else in the document, it acts differently. These are two different button clicks on the menu to call the event handler which inserts into the document.

On the first insert, here is a part of the exported xaml:

<t:Span FontFamily="Arial" FontSize="26.67" FontWeight="Bold" Text="This is the " />
<t:Span FontFamily="Arial" FontSize="26.67" FontStyle="Normal" FontWeight="Bold" ForeColor="#FF000000" HighlightColor="#00FFFFFF" Strikethrough="False" Text="Ams Field: " />
<custom1:AMSBindableFieldRangeStart AMSFieldName="AMS_FIELD_0" AnnotationID="1" Name="AMSBoundField_0" />
<t:Span FontFamily="Arial" FontSize="26.67" FontStyle="Normal" FontWeight="Bold" ForeColor="#FF000000" HighlightColor="#00FFFFFF" Strikethrough="False" Text="AMS_FIELD_0" UnderlineDecoration="Wave" />
<custom1:AMSBindableFieldRangeEnd AnnotationID="1" />
<t:Span FontFamily="Arial" FontSize="26.67" FontWeight="Bold" Text=" first sentence. This is the " />

The original text was "This is the first sentence." Then I inserted the fragment between 'the' and 'first'. This one turned out fine.

Here is theoutput from the exported xaml for the second insert:

<t:Span FontFamily="Arial" FontSize="26.67" FontWeight="Bold" Text=" first sentence. This is the " />
      <t:Span BaselineAlignment="Baseline" FontFamily="Arial" FontSize="16" FontStyle="Normal" FontWeight="Bold" ForeColor="#FF000000" HighlightColor="#00FFFFFF" Strikethrough="False" Text="Ams Field: " ThemeForeColor="none" ThemeUnderlineColor="none" UnderlineColor="#FF000000" UnderlineDecoration="None" />
      <custom1:AMSBindableFieldRangeStart AMSFieldName="AMS_FIELD_1" AnnotationID="2" Name="AMSBoundField_1" />
      <t:Span BaselineAlignment="Baseline" FontFamily="Arial" FontSize="16" FontStyle="Normal" FontWeight="Bold" ForeColor="#FF000000" HighlightColor="#00FFFFFF" Strikethrough="False" Text="AMS_FIELD_1" ThemeForeColor="none" ThemeUnderlineColor="none" UnderlineColor="#FF000000" UnderlineDecoration="Wave" />
      <custom1:AMSBindableFieldRangeEnd AnnotationID="2" />
      <t:Span FontFamily="Arial" FontSize="26.67" FontWeight="Bold" Text=" second sentence." />

As you can see, it adds a BaselineAlignment as well as several other properties to the inserted Spans that didn't exist in the first inserted fragment.

I get the same results if I use InsertFragment to insert a single span (no annotations) to the document. I am using the out of the box RadRichTextBox control with the context menu and RadRichTextBoxRibbonUI. I added an element to the ribbon which calls an event handler that creates a document, adds the section, paragraph, and spans, and then calls InsertFragment on the main document.

Please see the following source for the backing class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Telerik.Windows.Documents.Model;
using Telerik.Windows.Documents;
using Telerik.Windows.Controls;
using SLDetailConfigurationManager.Utils;
using Telerik.Windows.Documents.UI.TextDecorations.DecorationProviders;
using Telerik.Windows.Documents.Model.Styles;
 
namespace SLDetailConfigurationManager {
    public partial class MainPage : UserControl {
 
        private static int _controlId = 0;
 
        public MainPage() {
            InitializeComponent();
        }
 
        private void ApplyExistingStyleToSpan(ref Span span) {
            span.CopyPropertiesFromStyle(rtbMain.CurrentEditingStyle);
            span.FontFamily = rtbMain.CurrentEditingStyle.SpanProperties.FontFamily;
            span.FontSize = rtbMain.CurrentEditingStyle.SpanProperties.FontSize;
            span.FontStyle = rtbMain.CurrentEditingStyle.SpanProperties.FontStyle;
            span.FontWeight = rtbMain.CurrentEditingStyle.SpanProperties.FontWeight;
            span.ForeColor = rtbMain.CurrentEditingStyle.SpanProperties.ForeColor;
            span.HighlightColor = rtbMain.CurrentEditingStyle.SpanProperties.HighlightColor;
            span.Strikethrough = rtbMain.CurrentEditingStyle.SpanProperties.Strikethrough;
        }
 
        private void btnInsertAMSLabel_Click(object sender, RoutedEventArgs e) {
            int controlId = _controlId++;
 
            RadDocument document = new RadDocument();
            Section section = new Section();
            Paragraph paragraph = new Paragraph();
            Span label = new Span();
            Span bound = new Span();
 
            label.Text = "Ams Field: ";
            ApplyExistingStyleToSpan(ref label);
 
            AMSBindableFieldRangeStart start = new AMSBindableFieldRangeStart("AMSBoundField_" + controlId.ToString(), "AMS_FIELD_" + controlId.ToString());
            AMSBindableFieldRangeEnd end = new AMSBindableFieldRangeEnd();
            end.PairWithStart(start);
 
            bound.Text = "AMS_FIELD_" + controlId.ToString();
            ApplyExistingStyleToSpan(ref bound);
            bound.UnderlineDecoration = UnderlineTypes.Wave;
 
            paragraph.Inlines.Add(label);
            paragraph.Inlines.Add(start);
            paragraph.Inlines.Add(bound);
            paragraph.Inlines.Add(end);
 
            section.Blocks.Add(paragraph);
 
            document.Sections.Add(section);
 
            document.MeasureAndArrangeInDefaultSize();
            document.Selection.SelectAll();
 
            DocumentFragment frag = document.Selection.CopySelectedDocumentElements();
 
            rtbMain.InsertFragment(frag);
 
            rtbMain.Document.MeasureAndArrangeInDefaultSize();
 
        }
    }
}

Any ideas on what may be causing the second call to btnInsertAMSLabel_Click to insert the fragment differently?

Thanks,
Tyson

1 Answer, 1 is accepted

Sort by
0
Iva Toteva
Telerik team
answered on 05 Jun 2012, 06:47 PM
Hello Tyson,

Unfortunately, I was not able to reproduce the problem with the custom annotation ranges I created to mock-up the classes. Please, attach a sample project illustrating the issue in the support ticket you have opened and quote the exact version of the controls you are using, so that we can debug the issue and provide further assistance?

Looking forward to your reply in the support ticket. 

Greetings,
Iva Toteva
the Telerik team

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

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