TextRange 类

定义

表示两个 TextPointer 位置之间的所选内容。Represents a selection of content between two TextPointer positions.

public ref class TextRange
public class TextRange
type TextRange = class
Public Class TextRange
继承
TextRange
派生

示例

下面的示例演示如何使用的 Text 属性 TextRange 来返回指定的纯文本表示形式 FlowDocumentThe 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

注解

TextRange类引入了以下术语。The TextRange class introduces the following terminology.

  • 选择 -a TextRangetextpointer 指示的两个位置之间的内容选择。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.

  • 当前选择-由于 TextRange 始终指示内容中的选定内容,因此只需引用由表示的选定内容即可 TextRangeCurrent 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.

  • 文本容器 - 文本容器 是窗体上的流内容最终边框的元素;指示的选择 TextRange 始终落在文本容器中。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. 当前,文本容器必须是 FlowDocumentTextBlockCurrently, 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.

有关 位置 等其他相关术语的详细信息,请参阅 TextPointerFor more information about other related terms like position, see TextPointer.

构造函数

TextRange(TextPointer, TextPointer)

初始化 TextRange 类的新实例,将两个指定的 TextPointer 位置作为新范围的开始和结束位置。Initializes a new instance of the TextRange class, taking two specified TextPointer positions as the beginning and end positions for the new range.

属性

End

获取用于标记当前所选内容的结尾的位置。Get the position that marks the end of the current selection.

IsEmpty

获取一个值,该值指示当前所选内容是否为空。Gets a value indicating whether or not the current selection is empty.

Start

获取用于标记当前所选内容的开始的位置。Gets the position that marks the beginning of the current selection.

Text

获取或设置当前所选内容的纯文本内容。Gets or sets the plain text contents of the current selection.

方法

ApplyPropertyValue(DependencyProperty, Object)

将指定的格式属性和值应用于当前所选内容。Applies a specified formatting property and value to the current selection.

CanLoad(String)

检查当前所选内容是否可以与采用指定数据格式的内容一起加载。Checks whether the current selection can be loaded with content in a specified data format.

CanSave(String)

检查是否可将当前所选内容保存为指定数据格式。Checks whether the current selection can be saved as a specified data format.

ClearAllProperties()

从当前所选内容中移除所有格式属性(由 Inline 元素表示)。Removes all formatting properties (represented by Inline elements) from the current selection.

Contains(TextPointer)

检查某个位置(由 TextPointer 指定)是否位于当前所选内容中。Checks whether a position (specified by a TextPointer) is located within the current selection.

Equals(Object)

确定指定对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetPropertyValue(DependencyProperty)

返回对当前所选内容指定的格式属性的有效值。Returns the effective value of a specified formatting property on the current selection.

GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
Load(Stream, String)

从指定流加载采用指定数据格式的当前所选内容。Loads the current selection in a specified data format from a specified stream.

MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
Save(Stream, String)

将当前所选内容以指定数据格式保存到指定流中。Saves the current selection to a specified stream in a specified data format.

Save(Stream, String, Boolean)

将当前所选内容以指定数据格式保存到指定流中,同时选择保留自定义 TextElement 对象。Saves the current selection to a specified stream in a specified data format, with the option of preserving custom TextElement objects.

Select(TextPointer, TextPointer)

更新当前所选内容,使用两个 TextPointer 位置来指示更新的所选内容。Updates the current selection, taking two TextPointer positions to indicate the updated selection.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)

事件

Changed

在确定区域以覆盖新的内容范围时发生。Occurs when the range is repositioned to cover a new span of content.

适用于

另请参阅