xaml-csharp-development-skill-for-avalonia

TextBox Editing, Clipboard, Undo/Redo, and Text Input Options

Table of Contents

  1. Scope and APIs
  2. Baseline TextBox Authoring
  3. Selection and Caret Control
  4. Clipboard Pipeline (Cut, Copy, Paste)
  5. Undo/Redo and Edit History
  6. Text Input Options for Virtual Keyboard/IME
  7. Programmatic Patterns in C#
  8. Best Practices
  9. Troubleshooting

Scope and APIs

Primary APIs:

Important TextBox members:

Important TextInputOptions attached properties:

Reference source files:

Baseline TextBox 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.

Selection and Caret Control

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.

Clipboard Pipeline (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:

Undo/Redo and Edit History

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();
}

Text Input Options for Virtual Keyboard/IME

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(...).

Programmatic Patterns in C#

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);
}

Best Practices

Troubleshooting

  1. Undo/redo never activates.
    • Check IsUndoEnabled and avoid replacing Text on every keystroke from external code.
  2. Paste command disabled unexpectedly.
    • Verify IsReadOnly, selection state, and clipboard availability (CanPaste).
  3. Virtual keyboard type does not match intent.
    • Confirm TextInputOptions.ContentType and ReturnKeyType are set on the focused control.
  4. Text wrapping not applied.
    • Ensure AcceptsReturn="True" for multiline editing and set TextWrapping explicitly.
  5. Clipboard event not firing.
    • Subscribe to CopyingToClipboard, CuttingToClipboard, and PastingFromClipboard on the actual active TextBox instance.