Span Constructor (TextPointer, TextPointer)

 

Initializes a new instance of the Span class, taking two TextPointer objects that indicate the beginning and end of a selection of content that the new Span will contain.

Namespace:   System.Windows.Documents
Assembly:  PresentationFramework (in PresentationFramework.dll)

public Span(
	TextPointer start,
	TextPointer end
)

Parameters

start
Type: System.Windows.Documents.TextPointer

A TextPointer that indicates the beginning of a selection of content that the new Span will contain.

end
Type: System.Windows.Documents.TextPointer

A TextPointer that indicates the end of a selection of content that the new Span will contain.

Exception Condition
ArgumentNullException

Raised when start or end is null.

ArgumentException

Raised when start and end do not resolve to a range of content suitable for enclosure by a Span element; for example, if start and end indicate positions in different paragraphs.

This constructor is intended to initialize a new Span element such that it encloses a selection of pre-existing content.

The following example shows how to use this constructor to create a new Span that is applied to a range of content. The range of content is determined by the start and end TextPointer objects specified by the constructor.

// Create a paragraph and three text runs to serve as example content.  
Paragraph parx = new Paragraph();
Run run1 = new Run("Text run 1.");
Run run2 = new Run("Text run 2.");
Run run3 = new Run("Text run 3.");

// Add the three text runs to the paragraph, separated by linebreaks.
parx.Inlines.Add(run1);
parx.Inlines.Add(new LineBreak());
parx.Inlines.Add(run2);
parx.Inlines.Add(new LineBreak());
parx.Inlines.Add(run3);

// After this line executes, the selection of content
// indicated by the "start" and "end" parameters will be
// enclosed by the new Span.  In this case, the new Span
// will enclose the entire contents of the Paragraph "parx",
// which happens to contain three text runs and two linebreaks.               
Span spanx = new Span(parx.ContentStart, parx.ContentEnd);

// Now, properties set on "spanx" will override default properties
// on elements contained by "spanx".  For example, setting 
// these arbitrary display properties on "spanx" will affect
// the child text runs enclosed by "spanx".
spanx.Foreground = Brushes.Blue;
spanx.Background = Brushes.GhostWhite;
spanx.FontFamily = new FontFamily("Century Gothic");

// Non-default property values override any settings on the 
// enclosing Span element.
run2.Foreground = Brushes.Red;
run2.Background = Brushes.AntiqueWhite;
run2.FontFamily = new FontFamily("Lucida Handwriting");

The following figure shows how this example renders in a FlowDocument.

Screenshot: Three text runs

.NET Framework
Available since 3.0
Return to top
Show: