Span.Span(TextPointer, TextPointer) Constructor

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
)
public Span (
	TextPointer start, 
	TextPointer end
)
public function Span (
	start : TextPointer, 
	end : TextPointer
)
You cannot use constructors in XAML.

Parameters

start

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

end

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

Exception typeCondition

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

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions

ADD
Show: