Table of Contents

Class TextBox

Namespace
Avalonia.Controls
Assembly
Avalonia.Controls.dll

Represents a control that can be used to display or edit unformatted text.

[TemplatePart("PART_TextPresenter", typeof(TextPresenter), IsRequired = true)]
[TemplatePart("PART_ScrollViewer", typeof(ScrollViewer))]
[PseudoClasses(new string[] { ":empty" })]
public class TextBox : TemplatedControl, INotifyPropertyChanged, IDataContextProvider, ILogical, IThemeVariantHost, IResourceHost, IResourceNode, IStyleHost, ISetLogicalParent, ISetInheritanceParent, ISupportInitialize, IStyleable, INamed, IInputElement, IDataTemplateHost, ISetterValue
Inheritance
TextBox
Implements
Derived
Inherited Members
Extension Methods

Constructors

TextBox()

public TextBox()

Fields

AcceptsReturnProperty

Defines the AcceptsReturn property

public static readonly StyledProperty<bool> AcceptsReturnProperty

Field Value

StyledProperty<bool>

AcceptsTabProperty

Defines the AcceptsTab property

public static readonly StyledProperty<bool> AcceptsTabProperty

Field Value

StyledProperty<bool>

CanCopyProperty

Defines the CanCopy property

public static readonly DirectProperty<TextBox, bool> CanCopyProperty

Field Value

DirectProperty<TextBox, bool>

CanCutProperty

Defines the CanCut property

public static readonly DirectProperty<TextBox, bool> CanCutProperty

Field Value

DirectProperty<TextBox, bool>

CanPasteProperty

Defines the CanPaste property

public static readonly DirectProperty<TextBox, bool> CanPasteProperty

Field Value

DirectProperty<TextBox, bool>

CanRedoProperty

Defines the CanRedo property

public static readonly DirectProperty<TextBox, bool> CanRedoProperty

Field Value

DirectProperty<TextBox, bool>

CanUndoProperty

Defines the CanUndo property

public static readonly DirectProperty<TextBox, bool> CanUndoProperty

Field Value

DirectProperty<TextBox, bool>

CaretBlinkIntervalProperty

Defines the CaretBlinkInterval property

public static readonly StyledProperty<TimeSpan> CaretBlinkIntervalProperty

Field Value

StyledProperty<TimeSpan>

CaretBrushProperty

Defines the CaretBrush property

public static readonly StyledProperty<IBrush?> CaretBrushProperty

Field Value

StyledProperty<IBrush>

CaretIndexProperty

Defines the CaretIndex property

public static readonly StyledProperty<int> CaretIndexProperty

Field Value

StyledProperty<int>

ClearSelectionOnLostFocusProperty

Defines the ClearSelectionOnLostFocus property

public static readonly StyledProperty<bool> ClearSelectionOnLostFocusProperty

Field Value

StyledProperty<bool>

CopyingToClipboardEvent

Defines the CopyingToClipboard event.

public static readonly RoutedEvent<RoutedEventArgs> CopyingToClipboardEvent

Field Value

RoutedEvent<RoutedEventArgs>

CuttingToClipboardEvent

Defines the CuttingToClipboard event.

public static readonly RoutedEvent<RoutedEventArgs> CuttingToClipboardEvent

Field Value

RoutedEvent<RoutedEventArgs>

HorizontalContentAlignmentProperty

Defines the HorizontalAlignment property.

public static readonly StyledProperty<HorizontalAlignment> HorizontalContentAlignmentProperty

Field Value

StyledProperty<HorizontalAlignment>

InnerLeftContentProperty

Defines the InnerLeftContent property

public static readonly StyledProperty<object?> InnerLeftContentProperty

Field Value

StyledProperty<object>

InnerRightContentProperty

Defines the InnerRightContent property

public static readonly StyledProperty<object?> InnerRightContentProperty

Field Value

StyledProperty<object>

IsInactiveSelectionHighlightEnabledProperty

public static readonly StyledProperty<bool> IsInactiveSelectionHighlightEnabledProperty

Field Value

StyledProperty<bool>

IsReadOnlyProperty

Defines the IsReadOnly property

public static readonly StyledProperty<bool> IsReadOnlyProperty

Field Value

StyledProperty<bool>

IsUndoEnabledProperty

Defines the IsUndoEnabled property

public static readonly StyledProperty<bool> IsUndoEnabledProperty

Field Value

StyledProperty<bool>

LetterSpacingProperty

Defines see LetterSpacing property.

public static readonly StyledProperty<double> LetterSpacingProperty

Field Value

StyledProperty<double>

LineHeightProperty

Defines see LineHeight property.

public static readonly StyledProperty<double> LineHeightProperty

Field Value

StyledProperty<double>

MaxLengthProperty

Defines the MaxLength property

public static readonly StyledProperty<int> MaxLengthProperty

Field Value

StyledProperty<int>

MaxLinesProperty

Defines the MaxLines property

public static readonly StyledProperty<int> MaxLinesProperty

Field Value

StyledProperty<int>

MinLinesProperty

Defines the MinLines property

public static readonly StyledProperty<int> MinLinesProperty

Field Value

StyledProperty<int>

NewLineProperty

Defines the NewLine property

public static readonly StyledProperty<string> NewLineProperty

Field Value

StyledProperty<string>

PasswordCharProperty

Defines the PasswordChar property

public static readonly StyledProperty<char> PasswordCharProperty

Field Value

StyledProperty<char>

PastingFromClipboardEvent

Defines the PastingFromClipboard event.

public static readonly RoutedEvent<RoutedEventArgs> PastingFromClipboardEvent

Field Value

RoutedEvent<RoutedEventArgs>

RevealPasswordProperty

Defines the RevealPassword property

public static readonly StyledProperty<bool> RevealPasswordProperty

Field Value

StyledProperty<bool>

SelectionBrushProperty

Defines the SelectionBrush property

public static readonly StyledProperty<IBrush?> SelectionBrushProperty

Field Value

StyledProperty<IBrush>

SelectionEndProperty

Defines the SelectionEnd property

public static readonly StyledProperty<int> SelectionEndProperty

Field Value

StyledProperty<int>

SelectionForegroundBrushProperty

Defines the SelectionForegroundBrush property

public static readonly StyledProperty<IBrush?> SelectionForegroundBrushProperty

Field Value

StyledProperty<IBrush>

SelectionStartProperty

Defines the SelectionStart property

public static readonly StyledProperty<int> SelectionStartProperty

Field Value

StyledProperty<int>

TextAlignmentProperty

Defines the TextAlignment property

public static readonly StyledProperty<TextAlignment> TextAlignmentProperty

Field Value

StyledProperty<TextAlignment>

TextChangedEvent

Defines the TextChanged event.

public static readonly RoutedEvent<TextChangedEventArgs> TextChangedEvent

Field Value

RoutedEvent<TextChangedEventArgs>

TextChangingEvent

Defines the TextChanging event.

public static readonly RoutedEvent<TextChangingEventArgs> TextChangingEvent

Field Value

RoutedEvent<TextChangingEventArgs>

TextProperty

Defines the Text property

public static readonly StyledProperty<string?> TextProperty

Field Value

StyledProperty<string>

TextWrappingProperty

public static readonly StyledProperty<TextWrapping> TextWrappingProperty

Field Value

StyledProperty<TextWrapping>

UndoLimitProperty

Defines the UndoLimit property

public static readonly StyledProperty<int> UndoLimitProperty

Field Value

StyledProperty<int>

UseFloatingWatermarkProperty

Defines the UseFloatingWatermark property

public static readonly StyledProperty<bool> UseFloatingWatermarkProperty

Field Value

StyledProperty<bool>

VerticalContentAlignmentProperty

Defines the VerticalAlignment property.

public static readonly StyledProperty<VerticalAlignment> VerticalContentAlignmentProperty

Field Value

StyledProperty<VerticalAlignment>

WatermarkProperty

Defines the Watermark property

public static readonly StyledProperty<string?> WatermarkProperty

Field Value

StyledProperty<string>

Properties

AcceptsReturn

Gets or sets a value that determines whether the TextBox allows and displays newline or return characters

public bool AcceptsReturn { get; set; }

Property Value

bool

AcceptsTab

Gets or sets a value that determins whether the TextBox allows and displays tabs

public bool AcceptsTab { get; set; }

Property Value

bool

CanCopy

Property for determining if the Copy command can be executed.

public bool CanCopy { get; }

Property Value

bool

CanCut

Property for determining if the Cut command can be executed.

public bool CanCut { get; }

Property Value

bool

CanPaste

Property for determining if the Paste command can be executed.

public bool CanPaste { get; }

Property Value

bool

CanRedo

Gets a value that indicates whether the redo stack has an action that can be redone

public bool CanRedo { get; }

Property Value

bool

CanUndo

Gets a value that indicates whether the undo stack has an action that can be undone

public bool CanUndo { get; }

Property Value

bool

CaretBlinkInterval

Gets or sets the caret blink rate

public TimeSpan CaretBlinkInterval { get; set; }

Property Value

TimeSpan

CaretBrush

Gets or sets a brush that is used for the text caret

public IBrush? CaretBrush { get; set; }

Property Value

IBrush

CaretIndex

Gets or sets the index of the text caret

public int CaretIndex { get; set; }

Property Value

int

ClearSelectionOnLostFocus

Gets or sets a value that determines whether the TextBox clears its selection after it loses focus.

public bool ClearSelectionOnLostFocus { get; set; }

Property Value

bool

CopyGesture

Gets a platform-specific KeyGesture for the Copy action

public static KeyGesture? CopyGesture { get; }

Property Value

KeyGesture

CutGesture

Gets a platform-specific KeyGesture for the Cut action

public static KeyGesture? CutGesture { get; }

Property Value

KeyGesture

HorizontalContentAlignment

Gets or sets the horizontal alignment of the content within the control.

public HorizontalAlignment HorizontalContentAlignment { get; set; }

Property Value

HorizontalAlignment

InnerLeftContent

Gets or sets custom content that is positioned on the left side of the text layout box

public object? InnerLeftContent { get; set; }

Property Value

object

InnerRightContent

Gets or sets custom content that is positioned on the right side of the text layout box

public object? InnerRightContent { get; set; }

Property Value

object

IsInactiveSelectionHighlightEnabled

Gets or sets a value that determines whether the TextBox shows a selection highlight when it is not focused.

public bool IsInactiveSelectionHighlightEnabled { get; set; }

Property Value

bool

IsReadOnly

Gets or sets a value whether this TextBox is read-only

public bool IsReadOnly { get; set; }

Property Value

bool

IsUndoEnabled

Property for determining whether undo/redo is enabled

public bool IsUndoEnabled { get; set; }

Property Value

bool

LetterSpacing

Gets or sets the spacing between characters

public double LetterSpacing { get; set; }

Property Value

double

LineHeight

Gets or sets the line height.

public double LineHeight { get; set; }

Property Value

double

MaxLength

Gets or sets the maximum number of characters that the TextBox can accept. This constraint only applies for manually entered (user-inputted) text.

public int MaxLength { get; set; }

Property Value

int

MaxLines

Gets or sets the maximum number of visible lines to size to.

public int MaxLines { get; set; }

Property Value

int

MinLines

Gets or sets the minimum number of visible lines to size to.

public int MinLines { get; set; }

Property Value

int

NewLine

Gets or sets which characters are inserted when Enter is pressed. Default: NewLine

public string NewLine { get; set; }

Property Value

string

PasswordChar

Gets or sets the char that should be used for password masking

public char PasswordChar { get; set; }

Property Value

char

PasteGesture

Gets a platform-specific KeyGesture for the Paste action

public static KeyGesture? PasteGesture { get; }

Property Value

KeyGesture

RevealPassword

Gets or sets whether text masked by PasswordChar should be revealed

public bool RevealPassword { get; set; }

Property Value

bool

SelectedText

Gets or sets the text selected in the TextBox

public string SelectedText { get; set; }

Property Value

string

SelectionBrush

Gets or sets a brush that is used to highlight selected text

public IBrush? SelectionBrush { get; set; }

Property Value

IBrush

SelectionEnd

Gets or sets the end position of the text selected in the TextBox

public int SelectionEnd { get; set; }

Property Value

int

Remarks

When the SelectionEnd is equal to SelectionStart, there is no selected text and it marks the caret position

SelectionForegroundBrush

Gets or sets a brush that is used for the foreground of selected text

public IBrush? SelectionForegroundBrush { get; set; }

Property Value

IBrush

SelectionStart

Gets or sets the starting position of the text selected in the TextBox

public int SelectionStart { get; set; }

Property Value

int

Text

Gets or sets the Text content of the TextBox

[Content]
public string? Text { get; set; }

Property Value

string

TextAlignment

Gets or sets the TextAlignment of the TextBox

public TextAlignment TextAlignment { get; set; }

Property Value

TextAlignment

TextWrapping

Gets or sets the TextWrapping of the TextBox

public TextWrapping TextWrapping { get; set; }

Property Value

TextWrapping

UndoLimit

Gets or sets the maximum number of items that can reside in the Undo stack

public int UndoLimit { get; set; }

Property Value

int

UseFloatingWatermark

Gets or sets a value indicating whether the Watermark will still be shown above the Text even after a text value is set.

public bool UseFloatingWatermark { get; set; }

Property Value

bool

VerticalContentAlignment

Gets or sets the vertical alignment of the content within the control.

public VerticalAlignment VerticalContentAlignment { get; set; }

Property Value

VerticalAlignment

Watermark

Gets or sets the placeholder or descriptive text that is displayed even if the Text property is not yet set.

public string? Watermark { get; set; }

Property Value

string

Methods

Clear()

Clears the text in the TextBox

public void Clear()

ClearSelection()

Clears the current selection, maintaining the CaretIndex

public void ClearSelection()

CoerceText(string?)

Coerces the current text.

protected virtual string? CoerceText(string? value)

Parameters

value string

The initial text.

Returns

string

A coerced text.

Remarks

This method also manages the internal undo/redo state whenever the text changes: if overridden, ensure that the base is called or undo/redo won't work correctly.

Copy()

Copies the current text onto the clipboard

public void Copy()

Cut()

Cuts the current text onto the clipboard

public void Cut()

GetLineCount()

Get the number of lines in the TextBox.

public int GetLineCount()

Returns

int

number of lines in the TextBox, or -1 if no layout information is available

Remarks

If Wrap == true, changing the width of the TextBox may change this value. The value returned is the number of lines in the entire TextBox, regardless of how many are currently in view.

MeasureOverride(Size)

Measures the control and its child elements as part of a layout pass.

protected override Size MeasureOverride(Size availableSize)

Parameters

availableSize Size

The size available to the control.

Returns

Size

The desired size for the control.

OnApplyTemplate(TemplateAppliedEventArgs)

Called when the control's template is applied. In simple terms, this means the method is called just before the control is displayed.

protected override void OnApplyTemplate(TemplateAppliedEventArgs e)

Parameters

e TemplateAppliedEventArgs

The event args.

OnAttachedToVisualTree(VisualTreeAttachmentEventArgs)

Called when the control is added to a rooted visual tree.

protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)

Parameters

e VisualTreeAttachmentEventArgs

The event args.

OnCreateAutomationPeer()

Returns a new, type-specific AutomationPeer implementation for the control.

protected override AutomationPeer OnCreateAutomationPeer()

Returns

AutomationPeer

The type-specific AutomationPeer implementation.

OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs)

Called when the control is removed from a rooted visual tree.

protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)

Parameters

e VisualTreeAttachmentEventArgs

The event args.

OnGotFocus(GotFocusEventArgs)

Called before the GotFocus event occurs.

protected override void OnGotFocus(GotFocusEventArgs e)

Parameters

e GotFocusEventArgs

The event args.

OnKeyDown(KeyEventArgs)

Called before the KeyDown event occurs.

protected override void OnKeyDown(KeyEventArgs e)

Parameters

e KeyEventArgs

The event args.

OnLostFocus(RoutedEventArgs)

Called before the LostFocus event occurs.

protected override void OnLostFocus(RoutedEventArgs e)

Parameters

e RoutedEventArgs

The event args.

OnPointerMoved(PointerEventArgs)

Called before the PointerMoved event occurs.

protected override void OnPointerMoved(PointerEventArgs e)

Parameters

e PointerEventArgs

The event args.

OnPointerPressed(PointerPressedEventArgs)

Called before the PointerPressed event occurs.

protected override void OnPointerPressed(PointerPressedEventArgs e)

Parameters

e PointerPressedEventArgs

The event args.

OnPointerReleased(PointerReleasedEventArgs)

Called before the PointerReleased event occurs.

protected override void OnPointerReleased(PointerReleasedEventArgs e)

Parameters

e PointerReleasedEventArgs

The event args.

OnPropertyChanged(AvaloniaPropertyChangedEventArgs)

Called when a avalonia property changes on the object.

protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)

Parameters

change AvaloniaPropertyChangedEventArgs

The property change details.

OnTextInput(TextInputEventArgs)

Called before the TextInput event occurs.

protected override void OnTextInput(TextInputEventArgs e)

Parameters

e TextInputEventArgs

The event args.

Paste()

Pastes the current clipboard text content into the TextBox

public void Paste()

Redo()

Reapplies the first item on the redo stack

public void Redo()

ScrollToLine(int)

Scroll the TextBox to the specified line index.

public void ScrollToLine(int lineIndex)

Parameters

lineIndex int

The line index to scroll to.

Exceptions

ArgumentOutOfRangeException

lineIndex is less than zero. -or - lineIndex is larger than or equal to the line count.

SelectAll()

Select all text in the TextBox

public void SelectAll()

Undo()

Undoes the first action in the undo stack

public void Undo()

UpdateDataValidation(AvaloniaProperty, BindingValueType, Exception?)

Called to update the validation state for properties for which data validation is enabled.

protected override void UpdateDataValidation(AvaloniaProperty property, BindingValueType state, Exception? error)

Parameters

property AvaloniaProperty

The property.

state BindingValueType

The current data binding state.

error Exception

The current data binding error, if any.

Events

CopyingToClipboard

Raised when content is being copied to the clipboard

public event EventHandler<RoutedEventArgs>? CopyingToClipboard

Event Type

EventHandler<RoutedEventArgs>

CuttingToClipboard

Raised when content is being cut to the clipboard

public event EventHandler<RoutedEventArgs>? CuttingToClipboard

Event Type

EventHandler<RoutedEventArgs>

PastingFromClipboard

Raised when content is being pasted from the clipboard

public event EventHandler<RoutedEventArgs>? PastingFromClipboard

Event Type

EventHandler<RoutedEventArgs>

TextChanged

Occurs asynchronously after text changes and the new text is rendered.

public event EventHandler<TextChangedEventArgs>? TextChanged

Event Type

EventHandler<TextChangedEventArgs>

TextChanging

Occurs synchronously when text starts to change but before it is rendered.

public event EventHandler<TextChangingEventArgs>? TextChanging

Event Type

EventHandler<TextChangingEventArgs>

Remarks

This event occurs just after the Text property value has been updated.