Requirement:
To add a list of document variables inside the document editor and the fields should display the results, instead of variable name.
Example: from the below code, when a field is added, the result should be "Description" inside the text editor not {DOCVARIABLE 1001}.
Code:
based on http://www.telerik.com/help/wpf/radrichtextbox-features-document-variables.html
public
MainWindow()
{
InitializeComponent();
DocumentVariableInfo documentInfo =
new
DocumentVariableInfo();
documentInfo.Name =
"1001"
;
documentInfo.Value =
"Description"
;
radRichTextBox1.Document.DocumentVariableList.Add(documentInfo);
DocumentVariableField docVariable1 =
new
DocumentVariableField() { DisplayMode = FieldDisplayMode.Result, VariableName =
"1001"
};
this
.radRichTextBox1.InsertField(docVariable1);
this
.radRichTextBox1.Document.ChangeAllFieldsDisplayMode(FieldDisplayMode.Result);
this
.radRichTextBox1.ChangeFieldDisplayMode(docVariable1.FieldStart, FieldDisplayMode.Result);
this
.radRichTextBox1.ChangeAllFieldsDisplayMode(FieldDisplayMode.Result);
}
Any help much appreciated.
Thanks,
5 Answers, 1 is accepted
As you can see in the article you referenced, the two ways to add a document variable to a document are as follows:
this
.editor.Document.DocumentVariables.Add(
"Name"
,
"Andrew Fuller"
);
this
.editor.Document.DocumentVariables[
"Job"
] =
"Software Engineer"
;
The code you've implemented does not indeed add a document variable. The DocumentVariableList property is not supposed to be used directly from code and doesn't allow adding entries to the dictionary that DocumentVariables is.
So, in order to create and insert a document variable you can change your code like this:
radRichTextBox1.Document.DocumentVariables.Add(
"1001"
,
"Description"
);
DocumentVariableField docVariable =
new
DocumentVariableField() { VariableName =
"1001"
};
this
.radRichTextBox1.InsertField(docVariable, FieldDisplayMode.Result);
I hope this helps! Let us know if you need further assistance.
Greetings,
Petya
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
Hi Petya,
Thanks for the reply.
I am Jon, from the same team working on the app. I have question related to the above code during update of fields.
When you add like 1000 'document variables' (which is our requirement in this case),
Then lets say, change only one variable at a time and would like to update only that field in the document.
Tried with the following code and didnt work.
public
MainWindow()
{
DocumentVariableField docVariable;
string
keyValue =
string
.Empty;
InitializeComponent();
for
(
int
i = 1; i <= 1000; i++)
{
keyValue = i.ToString(
"0000"
);
this
.radRichTextBox1.Document.DocumentVariables.Add(keyValue, keyValue);
docVariable =
new
DocumentVariableField() { DisplayMode = FieldDisplayMode.Result, VariableName = keyValue };
this
.radRichTextBox1.InsertField(docVariable, FieldDisplayMode.Result);
_listDocumentField.Add(docVariable);
this
.radRichTextBox1.Insert(
"\r\n"
);
}
}
List<DocumentVariableField> _listDocumentField =
new
List<DocumentVariableField>();
private
void
Button_Click(
object
sender, RoutedEventArgs e)
{
this
.radRichTextBox1.Document.DocumentVariables[
"0011"
] =
"This is an update V1"
+ Guid.NewGuid().ToString();
DocumentVariableField field = _listDocumentField.Find(a => a.VariableName ==
"0011"
);
if
(field !=
null
)
{
field.Update();
field.FieldStart.Update();
this
.radRichTextBox1.UpdateField(field.FieldStart);
}
}
But If i use the below code it works, but takes more than a minute on a i7 processor, which i think is not efficient. so it would be better to update only a single field at a time for a single change.
private
void
Button_Click4(
object
sender, RoutedEventArgs e)
{
this
.radRichTextBox1.UpdateAllFields();
}
Possible solution (which needs Telerik api): Like to have the function that is built-into the document. When you right click on the document variable field example '0011' the 'Update field' option is available which updates only the selected field and display the updated result. Please see the attachment.
Thanks for the helps,
Indeed, the UpdateAllFields() method may cause some diminishment in the performance when there is a large amount of fields in the document. Thus, I believe that updating the fields one at a time, as you suggested, would be the better approach.
As for the issue you are facing in this regard, you should not use the list of fields you've previously populated, as the fields in that list are not the same as those in the editor's document and updating them would not affect the layout of the document. Instead, I would suggest you find the respective FieldRangeStart of the document variable field in the document and update through it. You can revise your code as follows:
this
.radRichTextBox1.Document.DocumentVariables[
"0011"
] =
"This is an update V1"
+ Guid.NewGuid().ToString();
var field =
this
.radRichTextBox1.Document.EnumerateChildrenOfType<FieldRangeStart>().Where(x => x.Field.FieldTypeName ==
"DOCVARIABLE"
&& ((DocumentVariableField)x.Field).VariableName ==
"0011"
).FirstOrDefault();
if
(field !=
null
)
{
this
.radRichTextBox1.UpdateField(field);
}
I hope this helps! Do not hesitate to get back to us if you need further assistance.
Greetings,
Petya
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.
The individual document variable worked and after a consecutive calls to the above said update it crashed. Attached the screenshot image.
Kind Regards,
Thank you for getting back to us with this issue!
I was able to replicate it when executing the update of several fields in a short time-frame, is this also the case on your side?
Overall, it looks like the problem is caused by the spell checker and disabling it prevents the issue from occurring. We logged this for further revision and will fix it as soon as possible. In the meantime, I suggest you disable the spell checker in your project by setting the IsSpellCheckingEnabled property to false. Let us know if this stops the problem on your side as well.
I am sorry for the inconvenience. I added some Telerik points to your account as a gratitude for the report.
Greetings,
Petya
the Telerik team
Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.