i.e. the field is a 'question' that can be answered ar runtime, i.e a combo box or something else
I have a my questions defined in a DB
I use the propertypath property to display the 'Question' to the user, I'd like another property on a mergefield that I can store other data .. like a tag property, that is saved with the document etc
Is this possible??
We used to use TXTextControl and we use APPLICATION FIELDS rather than MERGEFIELDS as this lets us create and save a param list
Is there something similar avaliable?
11 Answers, 1 is accepted
You can use custom field inheriting from merge field and add some properties (as demonstrated in the attached project). You should verify if they are correctly preserved because our XAML serializer may not successfully serialize them depending of the custom property type:
public
class
CustomMergeField : MergeField
{
private
const
string
CustomFieldName =
"CustomFieldName"
;
static
CustomMergeField()
{
CodeBasedFieldFactory.RegisterFieldType(CustomMergeField.CustomFieldName, () => {
return
new
CustomMergeField(); });
}
[XamlSerializable]
public
string
Tag {
get
;
set
; }
public
override
string
FieldTypeName
{
get
{
return
CustomMergeField.CustomFieldName;
}
}
public
override
void
CopyPropertiesFrom(Field fromField)
{
CustomMergeField mergeField = fromField
as
CustomMergeField;
if
(mergeField !=
null
)
{
this
.Tag = mergeField.Tag;
}
base
.CopyPropertiesFrom(fromField);
}
public
override
Field CreateInstance()
{
return
new
CustomMergeField();
}
}
Regards,
Boby
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
Thanks I'll give that a try now
:)
We have tried this here with three text properties (string) and when we add the to the document it seems to work as we have some temp code that checks the fields are in the document correctly
We then save the document as DOCX
When we load it all of the text properties we created are null
Any ideas?
Thanks
Whereas the "normal" merge fields are persisted in docx, HTML and XAML, custom merge fields and custom annotation ranges can be persisted only in XAML. The solution Boby suggested shows how the document model can be extended to provide similar functionality, but the export and import will work only with the XAML serialization.
Once master/detail mail merge is genuinely implemented, we will adjust the format providers to persist the merge fields correctly in the case of master/detail mail merge with the other formats as well. The feature, however, is not scheduled for 2012 Q1, so it will not be implemented before Q2.
Iva Toteva
the Telerik team
Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>
I am trying to achieve this, I have created some properties in my custom MergeField class, and saved the document in XAML format to a byte array, with Unicode encoding on the load and save.
Please see code below based on your example for the custom properties. I'm not sure what I'm missing here.
When I use the format provider to import the XAML string, I get an error "The type 'SynopticMergeField' was not found."
Thanks in advance!
public
class
SynopticMergeField : MergeField
{
private
const
string
QuestionTextstr =
"QuestionText"
;
static
SynopticMergeField()
{
CodeBasedFieldFactory.RegisterFieldType(SynopticMergeField.QuestionTextstr, () => {
return
new
SynopticMergeField(); });
}
[XamlSerializable]
public
string
QuestionText {
get
;
set
; }
[XamlSerializable]
public
string
OriginalField {
get
;
set
; }
[XamlSerializable]
public
string
ID {
get
;
set
; }
[XamlSerializable]
public
string
FieldName {
get
;
set
; }
public
override
Field CreateInstance()
{
return
new
SynopticMergeField();
}
public
override
string
FieldTypeName
{
get
{
return
SynopticMergeField.QuestionTextstr;
}
}
public
override
void
CopyPropertiesFrom(Field fromField)
{
SynopticMergeField mergeField = fromField
as
SynopticMergeField;
if
(mergeField !=
null
)
{
this
.QuestionText = mergeField.QuestionText;
this
.OriginalField = mergeField.OriginalField;
this
.ID = mergeField.ID;
this
.FieldName = mergeField.FieldName;
}
base
.CopyPropertiesFrom(fromField);
}
}
The most probable reason for that error is that static constructors aren't executed until a member/property of that class is accessed. Therefore, it may happen so that the field is not registered when importing the document. Please make sure that it is registered before doing the import. If that proves not the be the issue, please contact us again to investigate further.
Regards,
Ivailo Karamanolev
the Telerik team
I realised it was a case of having the wrong namespace in the XAML generated from the document, by default it was coming back like this:
<t:RadDocument xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:t="clr-namespace:Telerik.Windows.Documents.Model;assembly=Telerik.Windows.Documents" xmlns:s="clr-namespace:Telerik.Windows.Documents.Model.Styles;assembly=Telerik.Windows.Documents"
<t:SynopticMergeField ... />
I have it working now with this workaround.
Thanks,
Mike.
I'm working with Mike to get this working,
Our current progress is we are getting all the data back for the mailmerge and are at the point now where we want the mailmerge to take place.
We are not setting a MailMergeDataSource, and want to manually mailmerge, replacing the mergefield with text but also keeping the mergefield in place.
We have the text we want to display, but we're not sure of how to go about setting it, without calling .MailMerge(false) or setting a datasource.
I did try setting the datasource to a list on my viewmodel, which contains columns like "Full Name", and the mergefield's property path is set to "Full Name" however when I right click the mergefield and update, it disappears from the document.
Thanks.
Let me address your questions separately.
@Mike:
With the 2011.3.1220 version of the controls, custom mail merge fields are exported in the following way:
<
t:FieldRangeStart
AnnotationID
=
"2"
>
<
t:CustomMergeField
DisplayMode
=
"Code"
PropertyPath
=
"Orders"
xmlns:t
=
"clr-namespace:SilverlightApplication1;assembly=SilvelrightApplication1"
/>
</
t:FieldRangeStart
>
provided that SilverlightApplication1 is the name of your application and the namespace of the field. Redefining the namespace on the custom merge field resolves the issues with the namespace, so you don't need to do any additional processing of the exported document.
@Cameron:
In order to be able to set the result of the evaluation of a merge field, you can override the GetResultFragment() of MergeField. In this way, you can perform the mail merge without getting the values for the result from the MailMergeSource, but in another way. Please find attached a demo illustrating how this can be done.
When it comes to your other concern, when you perform a mail merge, the merge fields are not removed from the document. Their display mode changes, but they are still present in the document. You can verify that by exporting and importing a document from the demo.
The only thing you should be wary when using such custom merge fields is that the UI for inserting a merge field will not work unless you set an items source to the MailMergeDataSource.
I hope this helps.
Kind regards,
Iva Toteva
the Telerik team
Thanks very much for your reply, this is working great!
One last question regarding our custom MergeField, is there any way to make them readonly after they've been dragged on as we do not want the users to be able to edit or remove them in the document.
We need them to be readonly only after our template has been loaded, not when the templates are being designed.
Thanks.
Fields don't have an option to become read-only by themselves, however, RadDocument offers some options to make elements read-only:
- Document protection: using this feature, you'll be able to insert ranges, where the user will be able to edit the document when the protection is enabled. When disabled, the user will be able to edit the entire document.
- Read-only ranges: this is the inverse, where you specify ranges which will be read-only for the user. This will probably be better suited for the purpose, as you can insert one read-only range around each field you insert, thus rendering it read-only.
Greetings,
Ivailo Karamanolev
the Telerik team