How to: Create Numbered List in Code
- 3 minutes to read
To create a numbered list in code, perform the following steps.
- Add a new AbstractNumberingList instance to the Document.AbstractNumberingLists collection.
- Specify a NumberingListBase.NumberingType property, so that it is equal to the NumberingType.MultiLevel value.
- For each list level, specify the paragraph characteristics using the ListLevel.ParagraphProperties property. Set the left indentation so that each level has a different offset from the left. Specify a hanging first line and set the first line indent value to provide enough space for a number that precedes the text.
- Set the ListLevelProperties.Start value that is the number from which to start the list.
- Specify a format used to display a number via the ListLevelProperties.DisplayFormatString property.
- Use the Add method of the collection of lists in the document (accessible via the Document.NumberingLists property) to add a list definition to the document. The parameter is the index of an abstract numbering list created previously. The Add method creates a pattern that can be applied to a paragraph so that the paragraph looks like a list item.
- To include a paragraph in a list, set the Paragraph.ListIndex property of a paragraph to the index of a list in the document and specify the Paragraph.ListLevel property.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/wpf-richedit-document-api-t213968.
document.BeginUpdate();
// Define an abstract list that is the pattern for lists used in the document.
AbstractNumberingList list = document.AbstractNumberingLists.Add();
list.NumberingType = NumberingType.MultiLevel;
// Specify parameters for each list level.
ListLevel level = list.Levels[0];
level.ParagraphProperties.LeftIndent = 150;
level.ParagraphProperties.FirstLineIndentType = ParagraphFirstLineIndent.Hanging;
level.ParagraphProperties.FirstLineIndent = 75;
level.Start = 1;
level.NumberingFormat = NumberingFormat.Decimal;
level.DisplayFormatString = "{0}";
level = list.Levels[1];
level.ParagraphProperties.LeftIndent = 300;
level.ParagraphProperties.FirstLineIndentType = ParagraphFirstLineIndent.Hanging;
level.ParagraphProperties.FirstLineIndent = 150;
level.Start = 1;
level.NumberingFormat = NumberingFormat.DecimalEnclosedParenthses;
level.DisplayFormatString = "{0}→{1}";
level = list.Levels[2];
level.ParagraphProperties.LeftIndent = 450;
level.ParagraphProperties.FirstLineIndentType = ParagraphFirstLineIndent.Hanging;
level.ParagraphProperties.FirstLineIndent = 220;
level.Start = 1;
level.NumberingFormat = NumberingFormat.LowerRoman;
level.DisplayFormatString = "{0}→{1}→{2}";
// Create a list for use in the document. It is based on a previously defined abstract list with ID = 0.
document.NumberingLists.Add(0);
document.EndUpdate();
document.AppendText("Line one\nLine two\nLine three\nLine four");
// Convert all paragraphs to list items of level 0.
document.BeginUpdate();
ParagraphCollection paragraphs = document.Paragraphs;
foreach (Paragraph pgf in paragraphs)
{
pgf.ListIndex = 0;
pgf.ListLevel = 0;
}
// Specify a different level for a certain paragraph.
document.Paragraphs[1].ListLevel = 1;
document.EndUpdate();