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)
public class TextRange

The TextRange type exposes the following members.

  NameDescription
Public methodTextRangeInitializes a new instance of the TextRange class, taking two specified TextPointer positions as the beginning and end positions for the new range.
Top
  NameDescription
Public propertyEndGet the position that marks the end of the current selection.
Public propertyIsEmptyGets a value indicating whether or not the current selection is empty.
Public propertyStartGets the position that marks the beginning of the current selection.
Public propertyTextGets or sets the plain text contents of the current selection.
Top
  NameDescription
Public methodApplyPropertyValueApplies a specified formatting property and value to the current selection.
Public methodCanLoadChecks whether the current selection can be loaded with content in a specified data format.
Public methodCanSaveChecks whether the current selection can be saved as a specified data format.
Public methodClearAllPropertiesRemoves all formatting properties (represented by Inline elements) from the current selection.
Public methodContainsChecks whether a position (specified by a TextPointer) is located within the current selection.
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetPropertyValueReturns the effective value of a specified formatting property on the current selection.
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodLoadLoads the current selection in a specified data format from a specified stream.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodSave(Stream, String)Saves the current selection to a specified stream in a specified data format.
Public methodSave(Stream, String, Boolean)Saves the current selection to a specified stream in a specified data format, with the option of preserving custom TextElement objects.
Public methodSelectUpdates the current selection, taking two TextPointer positions to indicate the updated selection.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top
  NameDescription
Public eventChangedOccurs when the range is repositioned to cover a new span of content.
Top

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;
}

.NET Framework

Supported in: 4.5.1, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2014 Microsoft. All rights reserved.