MailMerge ConditionalMergeFields, 1:n Relations, Application Resource Styles

2 posts, 0 answers
  1. Wolfgang
    Wolfgang avatar
    3 posts
    Member since:
    Jul 2012

    Posted 24 Jul 2012 Link to this post

    Hi Telerik team,

    We are evaluating your RichTextBox and MailMerge functionality in order to display read-only views.

    Some questions had arisen we would like to address to you:

    We use our own Condiational MergeFields to hide, for example, a paragraph if the value of a Mergefield is empty. This is relevant in label-value situations where you, in addition to the empty value field, want to hide the Label. The paragraphs with conditional MergeFields are removed after loading the document. However, this procedure requires the reload of the document after a MailMergeDataSource change. Do you have a different approach to implement this behavior, perhaps to hide a section or paragraph instead of removing?
    public static class ConditionalMergeFields
    public static void RemoveEmptyMergeFields(RadDocument document)
    foreach (var section in document.Sections)
    // enumerate paragraphs which contain empty ConditionalMergeFields
                foreach (var paragraph in section.EnumerateChildrenOfType<Paragraph>().ToList()
                      .Where(paragraph => paragraph.EnumerateChildrenOfType<
                            .Any(f => f.Field 
    is IConditionalMergeField && ((IConditionalMergeField) f.Field).IsEmpty)))


    In order to implement lists and 1:n relationships in Richtext Templates we use custom List MergeFields. In the overloaded GetResultFragment method a new RadDocument is created, mailmerged and returned as DocumentFragment. As the amount of List items grows, the MailMerge method of the RadDocument consumes some time (4 times about 1.2 seconds). Would you suggest a different approach?

    protected override DocumentFragment GetResultFragment()
        var subList = getList();
        var document = (RadDocument)SubDataDocument.CreateDeepCopy();
        document.MailMergeDataSource.ItemsSource = subList;
        if (subList.Count > 1)
            document = document.MailMerge(false);
        if (document.Sections.First.Headers != null && document.Sections.First.Headers.Default != null && document.Sections.First.Headers.Default.Body != null)
        return new DocumentFragment(document);

    Text Styling like font size and font color are specified to a predetermined value in the RadDocument Template. Is there a way to bind them to application resources in order to style and change them on the fly? StaticResource is not working since the XamlFormatProvider does not find the Resource during the XAML parse Process.


    Thank you in advance,


  2. Iva Toteva
    Iva Toteva avatar
    1319 posts

    Posted 27 Jul 2012 Link to this post

    Hello Wolfgang,

    Thank you for your interest in RadRichTextBox. Please see below the answers to your questions:

    1. Document elements (such as paragraphs and sections) cannot be hidden. The only solution with the current version would be to remove them when they should not be shown.

    We have, however, considered the label-merge field scenario you have described and we believe that it would be nice to have the option to "hide" the paragraph if the value of the field is empty. In MS Word for example, this can be achieved using the TextBeforeIfNotEmpty and TextAfterIfNotEmpty properties of the merge field. We have logged this feature and will probably implement it in one of the next releases.

    2. As for the styling of the merge fields, each field's formatting is saved using the respective properties of the Span between the FieldRangeStart and FieldRangeEnd elements:

    <t:FieldRangeStart AnnotationID="1">
      <t:MergeField DisplayMode="Code" PropertyPath="FirstName" />
    <t:Span FontFamily="Courier New" FontSize="22.6666669845581" ForeColor="#FFFF0000" Text="{}{MERGEFIELD FirstName}" />
    <t:FieldRangeEnd AnnotationID="1" />

    In order to apply some formatting to all merge fields in the document, you could iterate through the field range starts (when the field is MergeField), select the merge fields and change their formatting using the methods and commands of the editor. You can find more information on the mail-merge related API in this article.

    I hope this answers your questions.

    All the best,
    Iva Toteva
    the Telerik team

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

  3. DevCraft R3 2016 release webinar banner
Back to Top