Working with Content Controls
This article shows some examples of how you can insert content controls, access existing and modify their properties from the code.
Manipulate Existing Content Controls
Get the Content Controls
The content controls can be retrieved by using the EnumerateChildrenOfType() method of any document element.
Example 1: Get all content controls
IEnumerable<SdtRangeStart> content_controls = document.EnumerateChildrenOfType<SdtRangeStart>();
foreach (SdtRangeStart item in content_controls)
{
Console.WriteLine("Type: {0} ID:{1}", item.SdtProperties.Type, item.SdtProperties.ID);
}
Set Content Controls Properties
This example shows how one can iterate the items and add an item to an existing ComboBox content control.
Example 2: Adding items to a ComboBox or a DropDownList
foreach (SdtRangeStart item in content_controls)
{
if (item.SdtProperties.Type == SdtType.ComboBox)
{
ComboBoxProperties properties = item.SdtProperties as ComboBoxProperties;
ListItem newItem = new ListItem();
newItem.DisplayText = "New Item Text";
properties.Items.Add(newItem);
}
}
Insert or Remove Content Controls
New content controls can be inserted through the InsertStructuredDocumentTag method of RadFlowDocumentEditor.The method has several overloads that allow you to insert the content control by passing its type, properties, and relative document elements.
If adding content controls with the InsertStructuredDocumentTag(SdtType) method without specifying the content, the resulting document will only have the annotation range start and end of the control. The desired content should be manually added afterwards.
Example 3: Inserting a content control using content control type
RadFlowDocument document = new RadFlowDocument();
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
var dateContentControl = editor.InsertStructuredDocumentTag(SdtType.Date);
editor.MoveToInlineStart(dateContentControl.End);
editor.InsertText(DateTime.Now.ToString()); // Insert content
Example 4: Inserting a Rich Text content control using content control properties
SdtProperties sdtProperties = new SdtProperties(SdtType.RichText)
{
Alias = "AliasName",
Lock = Lock.SdtContentLocked,
};
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
editor.InsertStructuredDocumentTag(sdtProperties);
editor.MoveToInlineStart(control.End);
Run span = editor.InsertText("Rich Text Content Control"); // Insert content inside the content control
span.FontWeight = FontWeights.Bold; // Style the content
Example 5: Inserting a CheckBox content control using content control properties
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
CheckBoxProperties checkBoxProperties = new CheckBoxProperties();
SdtRangeStart sdt = editor.InsertStructuredDocumentTag(checkBoxProperties);
editor.MoveToInlineEnd(sdt);
char text = (char)checkBoxProperties.UncheckedState.CharacterCode;
editor.InsertText(text.ToString());
editor.MoveToInlineEnd(sdt.End);
Example 6: Remove a content control
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
editor.RemoveStructuredDocumentTag(contentControl); //this will delete the entire content control along with the value
// or
editor.RemoveStructuredDocumentTag(contentControl, false); //this will preserve the value
Example 7: Insert a content control to a specific position
When using the InsertStructuredDocumentTag() method and passing start and end elements, make sure that the elements are not already part of a content control. An exception to the rule are the rich text and repeating section content controls, which can fully contain other controls, with the restriction that they cannot intersect their ranges.
RadFlowDocument document = new RadFlowDocument();
RadFlowDocumentEditor editor = new RadFlowDocumentEditor(document);
var paragrpah1 = editor.InsertParagraph();
editor.InsertText("Content Control");
var paragraph2 = editor.InsertParagraph();
SdtProperties sdtProperties = new SdtProperties(SdtType.RichText)
{
Alias = "AliasName",
Lock = Lock.SdtContentLocked,
};
editor.InsertStructuredDocumentTag(sdtProperties, paragrpah1, paragraph2);