Skip to main content

TextSelection Class

Definition​

Assembly:Avalonia.Controls.Documents
Package:Avalonia.Controls.Documents

Represents a text selection within a document. Manages selection state with anchor and moving positions for intuitive selection extension.

public class TextSelection

Inheritance: object -> TextRange -> TextSelection

Remarks​

Inherits from Avalonia.Controls.Documents.TextModel.TextRange to share formatting and content operations.

Constructors​

NameDescription
TextSelectionInitializes a new instance of the Avalonia.Controls.TextSelection class.

TextSelection Constructor​

Initializes a new instance of the Avalonia.Controls.TextSelection class.

public TextSelection(Avalonia.Controls.Documents.TextModel.TextDocument document)

Parameters​

document Avalonia.Controls.Documents.TextModel.TextDocument

The text document containing the selection.

Methods​

NameDescription
ApplyPropertyValueNo summary available.
ClearAllPropertiesClears all inline formatting properties from the range. Content is preserved; only formatting is removed. Produces a single Avalonia.Controls.Documents.Undo.BlockSnapshotUndoUnit for atomic undo/redo. Inherited from TextRange.
ClearPropertyValueInherited from TextRange.
ClearSelectionClears the selection, placing the caret at the current caret position.
ClearSpringloadPropertiesClears all springloaded properties.
ClearSpringloadPropertyClears a springloaded property.
Contains (2 overloads)Checks if the range contains the specified pointer. Inherited from TextRange.
CreateSnapshotCreates an immutable, thread-safe snapshot of this range's document structure. The caller chooses a serializer to convert the snapshot to a specific format. Inherited from TextRange.
DeleteDeletes the selected content, then collapses the selection to the deletion point and resets direction.
DeleteCurrentBlock (2 overloads)Deletes the block this range starts in, and collapses the range onto the deletion point. Inherited from TextRange.
DeleteTextDeletes the content of this range. Removes whatever the range covers — characters, inline runs, paragraphs, list items, tables, sections — depending on which nodes the range fully encloses. When the deletion crosses block boundaries and both surviving boundary blocks are paragraphs, mergeBlocks controls whether the surviving fragments fuse into one paragraph. Manages transaction automatically. Inherited from TextRange.
DeleteTextAtDeletes text at the specified range relative to this range. Manages transaction automatically. Inherited from TextRange.
ExtendToExtends the selection to a new position while keeping the anchor fixed.
GetContainingBlockNo summary available.
GetPropertyValueInherited from TextRange.
GetSpringloadPropertiesGets all springloaded properties.
GetTextReturns the text content covered by this range. Inherited from TextRange.
InsertFootnoteReplaces the range content with a footnote: the anchor (its number as ordinary text) lands at the range position, splitting a run when it sits mid-run, and the note body is created in the document's trailing footnote container with a fresh id - selection delete, anchor, body, and the anchor renumbering are one undo unit. Manages transaction automatically. Inherited from TextRange.
InsertImageInherited from TextRange.
InsertPageNumberFieldReplaces the range content with a page-number field of kind: the field lands at the range position, splitting a run when it sits mid-run, and shows its cached result until a page layout resolves it (a band on a sheet renders the sheet's number) - selection delete and insertion are one undo unit. Manages transaction automatically. Inherited from TextRange.
InsertSnapshotInserts a document snapshot over the selection, then collapses the selection to the end of the inserted content and resets direction.
InsertTextAtInserts text at the specified offset relative to the range start. Manages transaction automatically. Inherited from TextRange.
LoadReplaces this range with the content of a stream. Inherited from TextRange.
MoveToMoves the caret to the specified position.
OverlapsChecks if this range overlaps with another range. Inherited from TextRange.
ReplaceText (2 overloads)Inherited from TextRange.
ReplaceTextAtReplaces text at the specified range relative to this range. Manages transaction automatically. Inherited from TextRange.
SaveWrites this range to a stream. Inherited from TextRange.
SelectSelects a range of text between two positions.
SelectAllSelects all content in the document, from Avalonia.Controls.Documents.TextModel.TextDocument.ContentStart to Avalonia.Controls.Documents.TextModel.TextDocument.ContentEnd. ContentEnd anchors to the document's final block, so a trailing empty paragraph is part of the selection (and survives copy).
SetImageAltTextSets the alternative text of an existing Avalonia.Controls.Documents.RichImage in a single undoable change. Inherited from TextRange.
SetImageSizeSets the display size of an existing Avalonia.Controls.Documents.RichImage in a single undoable change. Pass double.NaN for a dimension to use the image's intrinsic pixel size. Inherited from TextRange.
SetSpringloadPropertyNo summary available.
TogglePropertyValueNo summary available.
ToStringInherited from TextRange.
TryGetPropertyValueInherited from TextRange.
TryGetSpringloadPropertyNo summary available.

ApplyPropertyValue Method​

public void ApplyPropertyValue<T>(Avalonia.StyledProperty<T> property, T value)

Parameters​

property Avalonia.StyledProperty<T>

value T

Type Parameters​

T

ClearSelection Method​

Clears the selection, placing the caret at the current caret position.

public void ClearSelection()

ClearSpringloadProperties Method​

Clears all springloaded properties.

public void ClearSpringloadProperties()

ClearSpringloadProperty Method​

Clears a springloaded property.

public void ClearSpringloadProperty(Avalonia.AvaloniaProperty property)

Parameters​

property Avalonia.AvaloniaProperty

The property to clear.

Delete Method​

Deletes the selected content, then collapses the selection to the deletion point and resets direction.

public Avalonia.Controls.Documents.TextModel.TextPointer Delete()

Returns​

Avalonia.Controls.Documents.TextModel.TextPointer

The deletion-point pointer returned by the base operation.

Remarks​

The base Avalonia.Controls.Documents.TextModel.TextRange.Delete returns the deletion point but does not reset _isForwardSelection or re-anchor the selection, so cut used to resolve the caret to the wrong endpoint. Re-selecting the returned pointer fixes every delete/cut entry point.

ExtendTo Method​

Extends the selection to a new position while keeping the anchor fixed.

public void ExtendTo(Avalonia.Controls.Documents.TextModel.TextPointer position)

Parameters​

position Avalonia.Controls.Documents.TextModel.TextPointer

The new moving position.

GetContainingBlock Method​

public Avalonia.Controls.Documents.Block GetContainingBlock()

Returns​

Avalonia.Controls.Documents.Block

GetSpringloadProperties Method​

Gets all springloaded properties.

public System.Collections.Generic.IReadOnlyDictionary<Avalonia.AvaloniaProperty, object> GetSpringloadProperties()

Returns​

System.Collections.Generic.IReadOnlyDictionary<Avalonia.AvaloniaProperty, object>

A read-only view of springloaded properties.

InsertSnapshot Method​

Inserts a document snapshot over the selection, then collapses the selection to the end of the inserted content and resets direction.

public void InsertSnapshot(Avalonia.Controls.Documents.Serialization.Snapshot.DocumentSnapshot snapshot)

Parameters​

snapshot Avalonia.Controls.Documents.Serialization.Snapshot.DocumentSnapshot

The snapshot whose tree is grafted into the selection.

Remarks​

The base Avalonia.Controls.Documents.TextModel.TextRange.InsertSnapshot(Avalonia.Controls.Documents.Serialization.Snapshot.DocumentSnapshot) leaves the range spanning the grafted content and never touches selection direction, so paste used to leave a non-collapsed selection whose caret sat one line past the content. Collapsing here re-anchors the caret the same way keyboard edits do via Avalonia.Controls.TextSelection.MoveTo(Avalonia.Controls.Documents.TextModel.TextPointer,bool), fixing every paste entry point (toolbar actions and keyboard).

MoveTo Method​

Moves the caret to the specified position.

public void MoveTo(Avalonia.Controls.Documents.TextModel.TextPointer position, bool extendSelection)

Parameters​

position Avalonia.Controls.Documents.TextModel.TextPointer

The new caret position.

extendSelection bool

If true, extends selection from anchor to the new position. If false, clears selection and moves both anchor and caret.

Select Method​

Selects a range of text between two positions.

public void Select(Avalonia.Controls.Documents.TextModel.TextPointer anchor, Avalonia.Controls.Documents.TextModel.TextPointer moving)

Parameters​

anchor Avalonia.Controls.Documents.TextModel.TextPointer

The anchor position where the selection starts.

moving Avalonia.Controls.Documents.TextModel.TextPointer

The moving end position that follows user input.

SelectAll Method​

Selects all content in the document, from Avalonia.Controls.Documents.TextModel.TextDocument.ContentStart to Avalonia.Controls.Documents.TextModel.TextDocument.ContentEnd. ContentEnd anchors to the document's final block, so a trailing empty paragraph is part of the selection (and survives copy).

public void SelectAll()

SetSpringloadProperty Method​

public void SetSpringloadProperty<T>(Avalonia.StyledProperty<T> property, T value)

Parameters​

property Avalonia.StyledProperty<T>

value T

Type Parameters​

T

TogglePropertyValue Method​

public void TogglePropertyValue<T>(Avalonia.StyledProperty<T> property, T onValue, T offValue)

Parameters​

property Avalonia.StyledProperty<T>

onValue T

offValue T

Type Parameters​

T

TryGetSpringloadProperty Method​

public bool TryGetSpringloadProperty<T>(Avalonia.StyledProperty<T> property, T& value)

Parameters​

property Avalonia.StyledProperty<T>

value T&

Type Parameters​

T

Returns​

bool

Properties​

NameDescription
AnchorPositionGets the anchor position where the selection started.
CaretPositionGets the caret position (the moving end that follows user input during selection).
EndGets the end position of the range. Inherited from TextRange.
IsEmptyGets a value indicating whether the range is empty. A range is empty only when Avalonia.Controls.Documents.TextModel.TextRange.Start and Avalonia.Controls.Documents.TextModel.TextRange.End refer to the exact same document position (node-aware). Two distinct positions that happen to share the same integer offset — for example pointers into consecutive empty paragraphs — are not considered empty. Inherited from TextRange.
LengthGets the length of the range in characters. Inherited from TextRange.
StartGets the start position of the range. Inherited from TextRange.
TextDocumentGets the document containing this range. Inherited from TextRange.

AnchorPosition Property​

Gets the anchor position where the selection started.

public Avalonia.Controls.Documents.TextModel.TextPointer AnchorPosition { get; set; }

CaretPosition Property​

Gets the caret position (the moving end that follows user input during selection).

public Avalonia.Controls.Documents.TextModel.TextPointer CaretPosition { get; set; }

Events​

NameDescription
ChangedOccurs when the range changes. Inherited from TextRange.