TextRange TextRange TextRange TextRange Class

Definition

Represents a selection of content between two TextPointer positions.

public ref class TextRange
public class TextRange
type TextRange = class
Public Class TextRange
Inheritance
TextRangeTextRangeTextRangeTextRange
Derived

Examples

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;
}
' This method returns a plain text representation of a specified FlowDocument.
Private Function GetTextFromFlowDocument(ByVal flowDoc As FlowDocument) As String
    ' Create a new TextRanage that takes the entire FlowDocument as the current selection.
    Dim flowDocSelection As 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
End Function

Remarks

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.

Constructors

TextRange(TextPointer, TextPointer) TextRange(TextPointer, TextPointer) TextRange(TextPointer, TextPointer) TextRange(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.

Properties

End End End End

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

IsEmpty IsEmpty IsEmpty IsEmpty

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

Start Start Start Start

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

Text Text Text Text

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

Methods

ApplyPropertyValue(DependencyProperty, Object) ApplyPropertyValue(DependencyProperty, Object) ApplyPropertyValue(DependencyProperty, Object) ApplyPropertyValue(DependencyProperty, Object)

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

CanLoad(String) CanLoad(String) CanLoad(String) CanLoad(String)

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

CanSave(String) CanSave(String) CanSave(String) CanSave(String)

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

ClearAllProperties() ClearAllProperties() ClearAllProperties() ClearAllProperties()

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

Contains(TextPointer) Contains(TextPointer) Contains(TextPointer) Contains(TextPointer)

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

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetPropertyValue(DependencyProperty) GetPropertyValue(DependencyProperty) GetPropertyValue(DependencyProperty) GetPropertyValue(DependencyProperty)

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

GetType() GetType() GetType() GetType()

Gets the Type of the current instance.

(Inherited from Object)
Load(Stream, String) Load(Stream, String) Load(Stream, String) Load(Stream, String)

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

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
Save(Stream, String) Save(Stream, String) Save(Stream, String) Save(Stream, String)

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

Save(Stream, String, Boolean) Save(Stream, String, Boolean) Save(Stream, String, Boolean) Save(Stream, String, Boolean)

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

Select(TextPointer, TextPointer) Select(TextPointer, TextPointer) Select(TextPointer, TextPointer) Select(TextPointer, TextPointer)

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

ToString() ToString() ToString() ToString()

Returns a string that represents the current object.

(Inherited from Object)

Events

Changed Changed Changed Changed

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

Applies to

See also