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 >>