TextRange Class
Collapse the table of content
Expand the table of content

TextRange Class


Represents a selection of content between two TextPointer positions.

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

public class TextRange

System_CAPS_pubmethodTextRange(TextPointer, TextPointer)

Initializes a new instance of the TextRange class, taking two specified TextPointer positions as the beginning and end positions for the new range.


Get the position that marks the end of the current selection.


Gets a value indicating whether or not the current selection is empty.


Gets the position that marks the beginning of the current selection.


Gets or sets the plain text contents of the current selection.

System_CAPS_pubmethodApplyPropertyValue(DependencyProperty, Object)

Applies a specified formatting property and value to the current selection.


Checks whether the current selection can be loaded with content in a specified data format.


Checks whether the current selection can be saved as a specified data format.


Removes all formatting properties (represented by Inline elements) from the current selection.


Checks whether a position (specified by a TextPointer) is located within the current selection.


Determines whether the specified object is equal to the current object.(Inherited from Object.)


Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)


Serves as the default hash function. (Inherited from Object.)


Returns the effective value of a specified formatting property on the current selection.


Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodLoad(Stream, String)

Loads the current selection in a specified data format from a specified stream.


Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_pubmethodSave(Stream, String)

Saves the current selection to a specified stream in a specified data format.

System_CAPS_pubmethodSave(Stream, String, Boolean)

Saves the current selection to a specified stream in a specified data format, with the option of preserving custom TextElement objects.

System_CAPS_pubmethodSelect(TextPointer, TextPointer)

Updates the current selection, taking two TextPointer positions to indicate the updated selection.


Returns a string that represents the current object.(Inherited from Object.)


Occurs when the range is repositioned to cover a new span of content.

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.

.NET Framework
Available since 3.0

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

Return to top
© 2015 Microsoft