Skip to main content
A control that can be used to display or edit unformatted text.

Namespace

Avalonia.Controls

Inheritance

ControlTemplatedControlTextBox

Properties

Text
string?
Gets or sets the text content of the TextBox. Supports two-way binding.
AcceptsReturn
bool
Gets or sets whether the TextBox allows and displays newline or return characters (multi-line mode).
AcceptsTab
bool
Gets or sets whether the TextBox allows and displays tabs.
IsReadOnly
bool
Gets or sets whether this TextBox is read-only.
PasswordChar
char
Gets or sets the character that should be used for password masking.
CaretIndex
int
Gets or sets the index of the text caret.
SelectionStart
int
Gets or sets the starting position of the text selected in the TextBox.
SelectionEnd
int
Gets or sets the end position of the text selected in the TextBox.
SelectedText
string
Gets or sets the text selected in the TextBox.
MaxLength
int
Gets or sets the maximum number of characters that the TextBox can accept. This constraint only applies for manually entered (user-inputted) text.
MaxLines
int
Gets or sets the maximum number of visible lines to size to.
MinLines
int
Gets or sets the minimum number of visible lines to size to.
TextAlignment
TextAlignment
Gets or sets the text alignment. Values: Left, Center, Right, Justify.
TextWrapping
TextWrapping
Gets or sets the text wrapping mode. Values: NoWrap, Wrap, WrapWithOverflow.
PlaceholderText
string?
Gets or sets the placeholder or descriptive text that is displayed even if the Text property is not yet set.
PlaceholderForeground
IBrush?
Gets or sets the brush used for the foreground color of the placeholder text.
UseFloatingPlaceholder
bool
Gets or sets whether the PlaceholderText will still be shown above the Text even after a text value is set.
SelectionBrush
IBrush?
Gets or sets a brush that is used to highlight selected text.
SelectionForegroundBrush
IBrush?
Gets or sets a brush that is used for the foreground of selected text.
CaretBrush
IBrush?
Gets or sets a brush that is used for the text caret.
IsUndoEnabled
bool
Gets or sets whether undo/redo is enabled. Default is true.
UndoLimit
int
Gets or sets the maximum number of items that can reside in the Undo stack.
CanCut
bool
Gets whether the Cut command can be executed. Read-only.
CanCopy
bool
Gets whether the Copy command can be executed. Read-only.
CanPaste
bool
Gets whether the Paste command can be executed. Read-only.
CanUndo
bool
Gets whether the undo stack has an action that can be undone. Read-only.
CanRedo
bool
Gets whether the redo stack has an action that can be redone. Read-only.

Events

TextChanged
EventHandler<TextChangedEventArgs>
Occurs asynchronously after text changes and the new text is rendered.
TextChanging
EventHandler<TextChangingEventArgs>
Occurs synchronously when text starts to change but before it is rendered. This event occurs just after the Text property value has been updated.
CopyingToClipboard
EventHandler<RoutedEventArgs>
Raised when content is being copied to the clipboard.
CuttingToClipboard
EventHandler<RoutedEventArgs>
Raised when content is being cut to the clipboard.
PastingFromClipboard
EventHandler<RoutedEventArgs>
Raised when content is being pasted from the clipboard.

Methods

Cut()
void
Cuts the current text onto the clipboard.
Copy()
void
Copies the current text onto the clipboard.
Paste()
void
Pastes the current clipboard text content into the TextBox.
ClearSelection()
void
Clears the current selection, maintaining the CaretIndex.
SelectAll()
void
Selects all text in the TextBox.
GetLineCount()
int
Gets the number of lines in the TextBox, or -1 if no layout information is available.

Template Parts

PART_TextPresenter
TextPresenter
required
The text presenter that displays the text. Required.
PART_ScrollViewer
ScrollViewer
Optional scroll viewer for scrolling text content.

Usage

XAML Example - Basic TextBox

<TextBox Text="{Binding UserName}" 
         PlaceholderText="Enter your name"
         Width="200" />

XAML Example - Multi-line TextBox

<TextBox AcceptsReturn="True"
         TextWrapping="Wrap"
         Height="100"
         PlaceholderText="Enter description..." />

XAML Example - Password TextBox

<TextBox PasswordChar="•"
         PlaceholderText="Enter password" />

XAML Example - Read-Only TextBox

<TextBox Text="{Binding Status}"
         IsReadOnly="True"
         Background="LightGray" />

C# Example - Text Binding

var textBox = new TextBox
{
    PlaceholderText = "Enter text here",
    MaxLength = 100
};

textBox.TextChanged += (sender, e) => 
{
    Debug.WriteLine($"Text changed to: {textBox.Text}");
};

C# Example - Selection

var textBox = new TextBox { Text = "Hello World" };

// Select "World"
textBox.SelectionStart = 6;
textBox.SelectionEnd = 11;

// Or select all
textBox.SelectAll();

// Get selected text
string selected = textBox.SelectedText; // "World"

Clipboard Operations

var textBox = new TextBox();

// Cut
if (textBox.CanCut)
{
    textBox.Cut();
}

// Copy
if (textBox.CanCopy)
{
    textBox.Copy();
}

// Paste
if (textBox.CanPaste)
{
    textBox.Paste();
}

Validation Example

<TextBox Text="{Binding Email}" 
         PlaceholderText="[email protected]">
  <DataValidationErrors.Error>
    <TextBlock Text="{Binding}" Foreground="Red" />
  </DataValidationErrors.Error>
</TextBox>

Source File

TextBox.cs:1-1400+

See Also

Build docs developers (and LLMs) love