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

RadRichTextBox MailMerge - Using Displayname to create a friendly name (with spaces)

4 Answers 213 Views
RichTextBox
This is a migrated thread and some comments may be shown as answers.
Joseph
Top achievements
Rank 1
Joseph asked on 25 Jun 2012, 11:53 AM

Hi all,

I think i'm missing something obvious...

I want to have some friendly names on my mail merge fields. (namely spaces in the field names)
So i have a class for instance


Public Class someclass
{
<DisplayName("This is the complex description of the field")>
Public property thisfieldnamehasacomplexdescription as string
  
Public property anothercomplexfield as string
  
}


This is the only way i know to get "Friendly" names in the dropdown that is the mail merge.
So the two fields turn up okay as :
"This is the complex description of the field" 
"anothercomplexfield"

but only 
anothercomplexfield  actually populates with data when you do the merge.

Am i going to have to template the raddropdownbutton that holds the mail merge fields?
Is there an example of this somewhere?


Also a sub question. How do i add a scroll bar??

4 Answers, 1 is accepted

Sort by
0
Iva Toteva
Telerik team
answered on 29 Jun 2012, 08:49 AM
Hi Joseph,

With the default MergeFields, it is not possible to change the display name fragment of the field in order to achieve a more friendly look. This should be possible if you implement a custom MergeField by deriving from the MergeField class. Here is a sample implementation that shows how this can be done:

public class CustomMergeField : MergeField
{
    private const string CustomFieldName = "CustomField";
 
    static CustomMergeField()
    {
        CodeBasedFieldFactory.RegisterFieldType(CustomMergeField.CustomFieldName, () => { return new CustomMergeField(); });
    }
 
    public override string FieldTypeName
    {
        get
        {
            return CustomMergeField.CustomFieldName;
        }
    }
 
    public override Field CreateInstance()
    {
        return new CustomMergeField();
    }
 
    protected override DocumentFragment GetDisplayNameFragment()
    {
        return base.CreateFragmentFromText(string.Format(Field.DisplayNameFragmentFormat, this.GetFriendlyFieldName(this.PropertyPath)));
    }
 
    private string GetFriendlyFieldName(string fieldName)
    {
        int lettersInEnglishAlphabet = 26;
        List<char> separators = new List<char>(lettersInEnglishAlphabet);
        for (int i = 0; i < lettersInEnglishAlphabet; i++)
        {
            separators.Add((char)('A' + i));
        }
        StringBuilder newFieldName = new StringBuilder();
        int previousIndex = 0;
        for (int i = 1; i < fieldName.Length; i++)
        {
            if (separators.Contains(fieldName[i]))
            {
                if (previousIndex > 0)
                {
                    newFieldName.Append(" ");
                }
                newFieldName.Append(fieldName.Substring(previousIndex, i - previousIndex));
                previousIndex = i;
            }
        }
        newFieldName.Append(" " + fieldName.Substring(previousIndex));
        return newFieldName.ToString();
    }
}

Note that the fragment that is shown when the DisplayMode is Code cannot be changed.

As for your other question, you can change the content of the dropdown button to show the friendly name of the fields and to include a scrollbar in the following way:
1. First, remove the binding of the button to the InsertMergeFieldEmptyCommand from XAML and give it a name (e.g. insertMergeField).
2. Next, add the following code in code-behind:
AddMergeFieldsInDropDownContent(this.insertMergeFieldButton);
 
private void AddMergeFieldsInDropDownContent(RadRibbonDropDownButton radRibbonDropDownButton)
{
    Grid grid = new Grid();
    grid.RowDefinitions.Add(new RowDefinition() { Height = new GridLength(100, GridUnitType.Pixel) });
 
    ScrollViewer scrollViewer = new ScrollViewer();
    scrollViewer.VerticalScrollBarVisibility = ScrollBarVisibility.Auto;
    StackPanel stackPanel = new StackPanel();
 
    foreach (string fieldName in this.editor.Document.MailMergeDataSource.GetColumnNames())
    {
        RadRibbonButton fieldButton = new RadRibbonButton()
        {
            Text = this.GetFriendlyFieldName(fieldName),
            Size = ButtonSize.Medium,
            HorizontalAlignment = HorizontalAlignment.Stretch,
            HorizontalContentAlignment = HorizontalAlignment.Left
        };
 
        fieldButton.Command = this.editor.Commands.InsertFieldCommand;
        fieldButton.CommandParameter = new MergeField() { PropertyPath = fieldName };
        //or
        //fieldButton.CommandParameter = new CustomMergeField() { PropertyPath = fieldName };
 
        stackPanel.Children.Add(fieldButton);
    }
    stackPanel.HorizontalAlignment = System.Windows.HorizontalAlignment.Stretch;
    scrollViewer.Content = stackPanel;
    grid.Children.Add(scrollViewer);
 
    radRibbonDropDownButton.DropDownContent = grid;
}

You can, of course optimize the code of the GetFriendlyName method and add it in a way that will be available by both classes.

I hope this helps.

Greetings,
Iva Toteva
the Telerik team

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

0
Clifford
Top achievements
Rank 1
answered on 11 Dec 2013, 02:03 AM
Could you create a sample to show this. Not sure I understand. I am creating a class right now with the mergefields as properties. This is very different from what I know.
0
Arthur
Top achievements
Rank 1
answered on 15 Aug 2014, 11:26 PM
We have exactly the same problem, there is no way we can specify "friendly names" for our merge fields.
I read your reply, but it would help if you could attach example to this thread, so instead of asking tons of questions we could figure it out from the working code. 

Thanks! 
0
Petya
Telerik team
answered on 18 Aug 2014, 01:32 PM
Hi Arthur,

Attached is the demo project you requested which uses the code from Iva's post. The demo also shows how you can wire a custom field to RadRichTextBox's predefined UI.

I hope you will find it useful.

Regards,
Petya
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
RichTextBox
Asked by
Joseph
Top achievements
Rank 1
Answers by
Iva Toteva
Telerik team
Clifford
Top achievements
Rank 1
Arthur
Top achievements
Rank 1
Petya
Telerik team
Share this question
or