我想我漏掉了一些明显的东西。我使用的是WPF的Telerik Rad控件,但我假设Rich text框对邮件合并功能使用了一些类似的实现。
我希望在我的邮件合并字段中有一些友好的名称。(即字段名称中的空格),例如,我有一个类
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 complex description of the field“”anothercomplexfield field“
但在进行合并时,只有另一个复杂字段会实际填充数据。
我是否必须为保存邮件合并字段的raddropdownbutton设置模板?有没有这样的例子?
还有一个子问题。如何在这些东西上添加滚动条?
(我还知道这块板不是TELERIK专用的板(讨厌!)但这可能会对将来的某些人有用。所以我会把我从Telerik得到的答案复制到这里!http://www.telerik.com/community/forums/wpf/richtextbox/558428-radrichtextbox-mailmerge---using-displayname-to-create-a-friendly-name-with-spaces.aspx )
发布于 2012-11-23 10:49:58
这是telerik给我的:
使用默认MergeFields时,不可能为了获得更友好的外观而更改字段的显示名称片段。如果您通过从MergeField类派生来实现自定义MergeField,这应该是可能的。下面是一个示例实现,展示了如何做到这一点:
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();
}
}请注意,当DisplayMode为代码时显示的片段不能更改。
至于您的另一个问题,您可以更改下拉按钮的内容,以显示字段的友好名称,并通过以下方式包含一个滚动条: 1.首先,从XAML中删除按钮到InsertMergeFieldEmptyCommand的绑定,并为其命名(例如insertMergeField)。2.接下来,在代码隐藏中添加以下代码:
AddMergeFieldsInDropDownContent(this.insertMergeFieldButton);
private void AddMergeFieldsInDropDownContent(RadRibbonDropDownButton radRibbonDropDownButton){ Grid grid = new Grid();grid.RowDefinitions.Add(new RowDefinition() {GridLength= 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;
}当然,您可以优化GetFriendlyName方法的代码,并以两个类都可用的方式添加它。
https://stackoverflow.com/questions/11199702
复制相似问题