Hi,
I want to "protect" mergefield inside my document, to prevent user to change the display of the field directly in the richtextbox(i use a specific editor to edit the value of my mergefield).
I tried to write a procedure to put or remove readonly on my mergefield, but I have some trouble to make it work :
I wonder if there are not some easier way to do this ? (Or I might be doing something wrong ?)
At the moment, I can only set/unset readonly ALL the mergefield of the document. It works, but sadly it's really slow ...
Are they're some way to "lock" the fields without using some ReadOnlyRange ?
Thanks.
I want to "protect" mergefield inside my document, to prevent user to change the display of the field directly in the richtextbox(i use a specific editor to edit the value of my mergefield).
I tried to write a procedure to put or remove readonly on my mergefield, but I have some trouble to make it work :
private
void
SetMergeFieldReadOnly(MergeField field, RadRichTextBox richTextBox,
bool
isReadonly)
{
var fieldEnd = richTextBox.Document.EnumerateChildrenOfType<FieldRangeEnd>()
.FirstOrDefault(fs => (fs.FieldRangeStart.Field
is
MergeField)
&& (((MergeField)fs.FieldRangeStart.Field).PropertyPath == field.PropertyPath));
if
(fieldEnd !=
null
)
{
var fieldStart = fieldEnd.FieldRangeStart;
var posFieldStart =
new
DocumentPosition(richTextBox.Document);
var posFieldEnd =
new
DocumentPosition(richTextBox.Document);
posFieldStart.MoveToInline(fieldStart);
posFieldEnd.MoveToInline(fieldEnd);
if
(isReadonly)
// add readonlyrange
{
richTextBox.Document.Selection.Clear();
richTextBox.Document.Selection.SetSelectionStart(posFieldStart);
richTextBox.Document.Selection.AddSelectionEnd(posFieldEnd);
richTextBox.InsertReadOnlyRange();
}
else
// remove readonlyrange
{
// 1st method - doesn't work.
//richTextBox.Document.Selection.Clear();
//richTextBox.Document.Selection.SetSelectionStart(posDebField);
//richTextBox.Document.Selection.AddSelectionEnd(posFinField);
//richTextBox.DeleteReadOnlyRange(); // <-- no error but the field is still readonly after that.
// 2nd method - works but not properly
var readOnlyRangeStarts = richTextBox.Document.EnumerateChildrenOfType<ReadOnlyRangeStart>();
var posDebReadOnly =
new
DocumentPosition(richTextBox.Document);
var posFinReadOnly =
new
DocumentPosition(richTextBox.Document);
foreach
(var readOnlyRangeStart
in
readOnlyRangeStarts)
{
if
(readOnlyRangeStart.End !=
null
)
{
posDebReadOnly.MoveToInline(readOnlyRangeStart);
posFinReadOnly.MoveToInline(readOnlyRangeStart.End);
if
((posFinReadOnly >= posFieldStart) && (posDebReadOnly <= posFieldEnd))
{
richTextBox.DeleteReadOnlyRange(readOnlyRangeStart);
// remove the protection on ALL the readonly range !!!
}
}
}
}
}
}
I wonder if there are not some easier way to do this ? (Or I might be doing something wrong ?)
At the moment, I can only set/unset readonly ALL the mergefield of the document. It works, but sadly it's really slow ...
Are they're some way to "lock" the fields without using some ReadOnlyRange ?
Thanks.