TextBox AuthoringCut, Copy, Paste)Primary APIs:
TextBoxTextInputOptionsTextInputContentTypeTextInputReturnKeyTypeInputElement.TextInputMethodClientRequestedEventImportant TextBox members:
Text, CaretIndex, SelectionStart, SelectionEnd, SelectedTextAcceptsReturn, AcceptsTab, TextWrappingIsReadOnly, PasswordChar, RevealPasswordMaxLength, MaxLines, MinLinesPlaceholderText, PlaceholderForeground, UseFloatingPlaceholderCanCut, CanCopy, CanPaste, CanUndo, CanRedoIsUndoEnabled, UndoLimitClearSelection(), SelectAll(), Clear(), ScrollToLine(int)Cut(), Copy(), Paste(), Undo(), Redo()CopyingToClipboard, CuttingToClipboard, PastingFromClipboardTextChanging, TextChangedCutGesture, CopyGesture, PasteGestureImportant TextInputOptions attached properties:
TextInputOptions.ContentTypeTextInputOptions.ReturnKeyTypeTextInputOptions.MultilineTextInputOptions.AutoCapitalizationTextInputOptions.IsSensitiveTextInputOptions.LowercaseTextInputOptions.UppercaseTextInputOptions.ShowSuggestionsReference source files:
src/Avalonia.Controls/TextBox.cssrc/Avalonia.Base/Input/TextInput/TextInputOptions.cssrc/Avalonia.Base/Input/TextInput/TextInputContentType.cssrc/Avalonia.Base/Input/TextInput/TextInputReturnKeyType.csTextBox Authoring<TextBox xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="360"
Text="{Binding Notes}"
AcceptsReturn="True"
TextWrapping="Wrap"
MaxLines="6"
PlaceholderText="Write a note..." />
Single-line defaults are usually enough for search/filter boxes. Use AcceptsReturn + TextWrapping for document-like input.
CaretIndex, SelectionStart, and SelectionEnd are style/binding-friendly properties.
using Avalonia.Controls;
void HighlightAll(TextBox textBox)
{
textBox.SelectAll();
}
void MoveCaretToEnd(TextBox textBox)
{
var textLength = textBox.Text?.Length ?? 0;
textBox.CaretIndex = textLength;
textBox.SelectionStart = textLength;
textBox.SelectionEnd = textLength;
}
Use ClearSelection() to collapse selection while preserving caret location semantics.
Cut, Copy, Paste)TextBox exposes routed clipboard events and imperative methods.
using Avalonia.Controls;
using Avalonia.Interactivity;
void WireClipboardGuards(TextBox textBox)
{
textBox.CopyingToClipboard += OnCopying;
textBox.CuttingToClipboard += OnCutting;
textBox.PastingFromClipboard += OnPasting;
}
void OnCopying(object? sender, RoutedEventArgs e)
{
// e.Handled = true; // enable if copy should be blocked.
}
void OnCutting(object? sender, RoutedEventArgs e)
{
}
void OnPasting(object? sender, RoutedEventArgs e)
{
}
Shortcut notes:
CutGesture, CopyGesture, PasteGesture expose platform-preferred defaults.KeyBindings, keep behavior consistent with these defaults.Use the built-in history rather than custom stacks where possible.
<TextBox xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Text="{Binding Script}"
IsUndoEnabled="True"
UndoLimit="200" />
void ApplyUndoRedo(TextBox textBox)
{
if (textBox.CanUndo)
textBox.Undo();
if (textBox.CanRedo)
textBox.Redo();
}
TextInputOptions configures keyboard and IME behavior via attached properties.
<TextBox xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="320"
TextInputOptions.ContentType="Email"
TextInputOptions.ReturnKeyType="Done"
TextInputOptions.Multiline="False"
TextInputOptions.AutoCapitalization="False"
TextInputOptions.ShowSuggestions="True"
PlaceholderText="name@example.com" />
Sensitive input pattern:
<TextBox xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
PasswordChar="*"
TextInputOptions.ContentType="Password"
TextInputOptions.IsSensitive="True"
TextInputOptions.ShowSuggestions="False" />
At runtime, effective options can be read with TextInputOptions.FromStyledElement(...).
using Avalonia.Controls;
using Avalonia.Input.TextInput;
void ConfigureInput(TextBox textBox)
{
TextInputOptions.SetContentType(textBox, TextInputContentType.Search);
TextInputOptions.SetReturnKeyType(textBox, TextInputReturnKeyType.Search);
TextInputOptions.SetShowSuggestions(textBox, true);
}
void MoveToLine(TextBox textBox, int line)
{
textBox.ScrollToLine(line);
}
Text with validation enabled (default for TextProperty).CanCut/CanCopy/CanPaste and CanUndo/CanRedo to drive command availability.TextInputOptions explicitly for mobile and tablet scenarios.PlaceholderText; keep Watermark aliases only for legacy compatibility.IsUndoEnabled and avoid replacing Text on every keystroke from external code.IsReadOnly, selection state, and clipboard availability (CanPaste).TextInputOptions.ContentType and ReturnKeyType are set on the focused control.AcceptsReturn="True" for multiline editing and set TextWrapping explicitly.CopyingToClipboard, CuttingToClipboard, and PastingFromClipboard on the actual active TextBox instance.