SdtListItemCollection Class

Provides access to SdtListItem elements of a structured document tag.
Inheritance Hierarchy
SystemObject
  Aspose.Words.MarkupSdtListItemCollection

Namespace:  Aspose.Words.Markup
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.3
Syntax
public class SdtListItemCollection : IEnumerable<SdtListItem>, 
	IEnumerable

The SdtListItemCollection type exposes the following members.

Properties
  NameDescription
Public propertyCode exampleCount
Gets number of items in the collection.
Public propertyCode exampleItem
Returns a SdtListItem object given its zero-based index in the collection.
Public propertyCode exampleSelectedValue
Specifies currently selected value in this list. Null value allowed, meaning that no currently selected entry is associated with this list item collection.
Methods
  NameDescription
Public methodCode exampleAdd
Adds an item to this collection.
Public methodCode exampleClear
Clears all items from this collection.
Public methodEquals (Inherited from Object.)
Public methodCode exampleGetEnumerator
Returns an enumerator object that can be used to iterate over all items in the collection.
Public methodGetHashCode (Inherited from Object.)
Public methodGetType (Inherited from Object.)
Public methodCode exampleRemoveAt
Removes a list item at the specified index.
Public methodToString (Inherited from Object.)
Examples
Shows how to work with StructuredDocumentTag nodes of the DropDownList type.
// Create a blank document and insert a StructuredDocumentTag that will contain a drop down list
Document doc = new Document();
StructuredDocumentTag tag = new StructuredDocumentTag(doc, SdtType.DropDownList, MarkupLevel.Block);
doc.FirstSection.Body.AppendChild(tag);

// A drop down list needs elements, each of which will be a SdtListItem
SdtListItemCollection listItems = tag.ListItems;
listItems.Add(new SdtListItem("Value 1"));

// Each SdtListItem has text that will be displayed when the drop down list is opened, and also a value
// When we initialize with one string, we are providing just the value
// Accordingly, value is passed as DisplayText and will consequently be displayed on the screen
Assert.AreEqual(listItems[0].DisplayText, listItems[0].Value);

// Add 3 more SdtListItems with non-empty strings passed to DisplayText
listItems.Add(new SdtListItem("Item 2", "Value 2"));
listItems.Add(new SdtListItem("Item 3", "Value 3"));
listItems.Add(new SdtListItem("Item 4", "Value 4"));

// We can obtain a count of the SdtListItems and also set the drop down list's SelectedValue attribute to
// automatically have one of them pre-selected when we open the document in Microsoft Word
Assert.AreEqual(4, listItems.Count);
listItems.SelectedValue = listItems[3];

Assert.AreEqual("Value 4", listItems.SelectedValue.Value);

// We can enumerate over the collection and print each element
using (IEnumerator<SdtListItem> enumerator = listItems.GetEnumerator())
{
    while (enumerator.MoveNext())
    {
        if (enumerator.Current != null)
            Console.WriteLine($"List item: {enumerator.Current.DisplayText}, value: {enumerator.Current.Value}");
    }
}

// We can also remove elements one at a time
listItems.RemoveAt(3);
Assert.AreEqual(3, listItems.Count);

// Make sure to update the SelectedValue's index if it ever ends up out of bounds before saving the document
listItems.SelectedValue = listItems[1];

doc.Save(ArtifactsDir + "StructuredDocumentTag.ListItemCollection.docx");

// We can clear the whole collection at once too
listItems.Clear();
Assert.AreEqual(0, listItems.Count);
See Also