Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

TextRange Class

Represents a selection of content between two TextPointer positions.

Namespace: System.Windows.Documents
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

public class TextRange
public class TextRange
public class TextRange
You cannot use this managed class in XAML.

The TextRange class introduces the following terminology.

  • Selection - A TextRange is a selection of content between two positions indicated by TextPointers. One of these positions a fixed anchor with respect to the selection, while the other position is movable. This is similar to how a selection made by a user using the mouse or keyboard behaves.

  • Current Selection - Because a TextRange always indicates a selection in content, it makes sense to simply refer to the selection indicated by a TextRange as the current selection.

  • Text Container - A text container is the element that forms the ultimate border for the flow content at hand; the selection indicated by a TextRange always falls within a text container. Currently, a text container must be either a FlowDocument or a TextBlock.

  • Document - The content collectively contained within a text container is referred to as a document.

For more information about other related terms like position, see TextPointer.

The following example demonstrates using the Text property of a TextRange to return the plain text representation of a specified FlowDocument.

// This method returns a plain text representation of a specified FlowDocument.
string GetTextFromFlowDocument(FlowDocument flowDoc)
{
     // Create a new TextRanage that takes the entire FlowDocument as the current selection.
     TextRange flowDocSelection = new TextRange(flowDoc.ContentStart, flowDoc.ContentEnd);
      
     // Use the Text property to extract a string that contains the unformatted text contents 
     // of the FlowDocument.
     return flowDocSelection.Text;
}

System.Object
  System.Windows.Documents.TextRange
     System.Windows.Documents.TextSelection

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

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
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

Show:
© 2015 Microsoft