Class TextBox
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
AcceptsTabProperty
Defines the AcceptsTab property
public static readonly StyledProperty<bool> AcceptsTabProperty
Field Value
CanCopyProperty
Defines the CanCopy property
public static readonly DirectProperty<TextBox, bool> CanCopyProperty
Field Value
CanCutProperty
Defines the CanCut property
public static readonly DirectProperty<TextBox, bool> CanCutProperty
Field Value
CanPasteProperty
Defines the CanPaste property
public static readonly DirectProperty<TextBox, bool> CanPasteProperty
Field Value
CanRedoProperty
Defines the CanRedo property
public static readonly DirectProperty<TextBox, bool> CanRedoProperty
Field Value
CanUndoProperty
Defines the CanUndo property
public static readonly DirectProperty<TextBox, bool> CanUndoProperty
Field Value
CaretBlinkIntervalProperty
Defines the CaretBlinkInterval property
public static readonly StyledProperty<TimeSpan> CaretBlinkIntervalProperty
Field Value
CaretBrushProperty
Defines the CaretBrush property
public static readonly StyledProperty<IBrush?> CaretBrushProperty
Field Value
CaretIndexProperty
Defines the CaretIndex property
public static readonly StyledProperty<int> CaretIndexProperty
Field Value
ClearSelectionOnLostFocusProperty
Defines the ClearSelectionOnLostFocus property
public static readonly StyledProperty<bool> ClearSelectionOnLostFocusProperty
Field Value
CopyingToClipboardEvent
Defines the CopyingToClipboard event.
public static readonly RoutedEvent<RoutedEventArgs> CopyingToClipboardEvent
Field Value
CuttingToClipboardEvent
Defines the CuttingToClipboard event.
public static readonly RoutedEvent<RoutedEventArgs> CuttingToClipboardEvent
Field Value
HorizontalContentAlignmentProperty
Defines the HorizontalAlignment property.
public static readonly StyledProperty<HorizontalAlignment> HorizontalContentAlignmentProperty
Field Value
InnerLeftContentProperty
Defines the InnerLeftContent property
public static readonly StyledProperty<object?> InnerLeftContentProperty
Field Value
InnerRightContentProperty
Defines the InnerRightContent property
public static readonly StyledProperty<object?> InnerRightContentProperty
Field Value
IsInactiveSelectionHighlightEnabledProperty
Defines the IsInactiveSelectionHighlightEnabled property
public static readonly StyledProperty<bool> IsInactiveSelectionHighlightEnabledProperty
Field Value
IsReadOnlyProperty
Defines the IsReadOnly property
public static readonly StyledProperty<bool> IsReadOnlyProperty
Field Value
IsUndoEnabledProperty
Defines the IsUndoEnabled property
public static readonly StyledProperty<bool> IsUndoEnabledProperty
Field Value
LetterSpacingProperty
Defines see LetterSpacing property.
public static readonly StyledProperty<double> LetterSpacingProperty
Field Value
LineHeightProperty
Defines see LineHeight property.
public static readonly StyledProperty<double> LineHeightProperty
Field Value
MaxLengthProperty
Defines the MaxLength property
public static readonly StyledProperty<int> MaxLengthProperty
Field Value
MaxLinesProperty
Defines the MaxLines property
public static readonly StyledProperty<int> MaxLinesProperty
Field Value
MinLinesProperty
Defines the MinLines property
public static readonly StyledProperty<int> MinLinesProperty
Field Value
NewLineProperty
Defines the NewLine property
public static readonly StyledProperty<string> NewLineProperty
Field Value
PasswordCharProperty
Defines the PasswordChar property
public static readonly StyledProperty<char> PasswordCharProperty
Field Value
PastingFromClipboardEvent
Defines the PastingFromClipboard event.
public static readonly RoutedEvent<RoutedEventArgs> PastingFromClipboardEvent
Field Value
RevealPasswordProperty
Defines the RevealPassword property
public static readonly StyledProperty<bool> RevealPasswordProperty
Field Value
SelectionBrushProperty
Defines the SelectionBrush property
public static readonly StyledProperty<IBrush?> SelectionBrushProperty
Field Value
SelectionEndProperty
Defines the SelectionEnd property
public static readonly StyledProperty<int> SelectionEndProperty
Field Value
SelectionForegroundBrushProperty
Defines the SelectionForegroundBrush property
public static readonly StyledProperty<IBrush?> SelectionForegroundBrushProperty
Field Value
SelectionStartProperty
Defines the SelectionStart property
public static readonly StyledProperty<int> SelectionStartProperty
Field Value
TextAlignmentProperty
Defines the TextAlignment property
public static readonly StyledProperty<TextAlignment> TextAlignmentProperty
Field Value
TextChangedEvent
Defines the TextChanged event.
public static readonly RoutedEvent<TextChangedEventArgs> TextChangedEvent
Field Value
TextChangingEvent
Defines the TextChanging event.
public static readonly RoutedEvent<TextChangingEventArgs> TextChangingEvent
Field Value
TextProperty
Defines the Text property
public static readonly StyledProperty<string?> TextProperty
Field Value
TextWrappingProperty
public static readonly StyledProperty<TextWrapping> TextWrappingProperty
Field Value
UndoLimitProperty
Defines the UndoLimit property
public static readonly StyledProperty<int> UndoLimitProperty
Field Value
UseFloatingWatermarkProperty
Defines the UseFloatingWatermark property
public static readonly StyledProperty<bool> UseFloatingWatermarkProperty
Field Value
VerticalContentAlignmentProperty
Defines the VerticalAlignment property.
public static readonly StyledProperty<VerticalAlignment> VerticalContentAlignmentProperty
Field Value
WatermarkProperty
Defines the Watermark property
public static readonly StyledProperty<string?> WatermarkProperty
Field Value
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
AcceptsTab
Gets or sets a value that determins whether the TextBox allows and displays tabs
public bool AcceptsTab { get; set; }
Property Value
CanCopy
Property for determining if the Copy command can be executed.
public bool CanCopy { get; }
Property Value
CanCut
Property for determining if the Cut command can be executed.
public bool CanCut { get; }
Property Value
CanPaste
Property for determining if the Paste command can be executed.
public bool CanPaste { get; }
Property Value
CanRedo
Gets a value that indicates whether the redo stack has an action that can be redone
public bool CanRedo { get; }
Property Value
CanUndo
Gets a value that indicates whether the undo stack has an action that can be undone
public bool CanUndo { get; }
Property Value
CaretBlinkInterval
Gets or sets the caret blink rate
public TimeSpan CaretBlinkInterval { get; set; }
Property Value
CaretBrush
Gets or sets a brush that is used for the text caret
public IBrush? CaretBrush { get; set; }
Property Value
CaretIndex
Gets or sets the index of the text caret
public int CaretIndex { get; set; }
Property Value
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
CopyGesture
Gets a platform-specific KeyGesture for the Copy action
public static KeyGesture? CopyGesture { get; }
Property Value
CutGesture
Gets a platform-specific KeyGesture for the Cut action
public static KeyGesture? CutGesture { get; }
Property Value
HorizontalContentAlignment
Gets or sets the horizontal alignment of the content within the control.
public HorizontalAlignment HorizontalContentAlignment { get; set; }
Property Value
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
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
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
IsReadOnly
Gets or sets a value whether this TextBox is read-only
public bool IsReadOnly { get; set; }
Property Value
IsUndoEnabled
Property for determining whether undo/redo is enabled
public bool IsUndoEnabled { get; set; }
Property Value
LetterSpacing
Gets or sets the spacing between characters
public double LetterSpacing { get; set; }
Property Value
LineHeight
Gets or sets the line height.
public double LineHeight { get; set; }
Property Value
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
MaxLines
Gets or sets the maximum number of visible lines to size to.
public int MaxLines { get; set; }
Property Value
MinLines
Gets or sets the minimum number of visible lines to size to.
public int MinLines { get; set; }
Property Value
NewLine
Gets or sets which characters are inserted when Enter is pressed. Default: NewLine
public string NewLine { get; set; }
Property Value
PasswordChar
Gets or sets the char that should be used for password masking
public char PasswordChar { get; set; }
Property Value
PasteGesture
Gets a platform-specific KeyGesture for the Paste action
public static KeyGesture? PasteGesture { get; }
Property Value
RevealPassword
Gets or sets whether text masked by PasswordChar should be revealed
public bool RevealPassword { get; set; }
Property Value
SelectedText
Gets or sets the text selected in the TextBox
public string SelectedText { get; set; }
Property Value
SelectionBrush
Gets or sets a brush that is used to highlight selected text
public IBrush? SelectionBrush { get; set; }
Property Value
SelectionEnd
Gets or sets the end position of the text selected in the TextBox
public int SelectionEnd { get; set; }
Property Value
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
SelectionStart
Gets or sets the starting position of the text selected in the TextBox
public int SelectionStart { get; set; }
Property Value
Text
Gets or sets the Text content of the TextBox
[Content]
public string? Text { get; set; }
Property Value
TextAlignment
Gets or sets the TextAlignment of the TextBox
public TextAlignment TextAlignment { get; set; }
Property Value
TextWrapping
Gets or sets the TextWrapping of the TextBox
public TextWrapping TextWrapping { get; set; }
Property Value
UndoLimit
Gets or sets the maximum number of items that can reside in the Undo stack
public int UndoLimit { get; set; }
Property Value
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
VerticalContentAlignment
Gets or sets the vertical alignment of the content within the control.
public VerticalAlignment VerticalContentAlignment { get; set; }
Property Value
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
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
stringThe 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
SizeThe 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
TemplateAppliedEventArgsThe event args.
OnAttachedToVisualTree(VisualTreeAttachmentEventArgs)
Called when the control is added to a rooted visual tree.
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
Parameters
e
VisualTreeAttachmentEventArgsThe 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
VisualTreeAttachmentEventArgsThe event args.
OnGotFocus(GotFocusEventArgs)
Called before the GotFocus event occurs.
protected override void OnGotFocus(GotFocusEventArgs e)
Parameters
e
GotFocusEventArgsThe event args.
OnKeyDown(KeyEventArgs)
Called before the KeyDown event occurs.
protected override void OnKeyDown(KeyEventArgs e)
Parameters
e
KeyEventArgsThe event args.
OnLostFocus(RoutedEventArgs)
Called before the LostFocus event occurs.
protected override void OnLostFocus(RoutedEventArgs e)
Parameters
e
RoutedEventArgsThe event args.
OnPointerMoved(PointerEventArgs)
Called before the PointerMoved event occurs.
protected override void OnPointerMoved(PointerEventArgs e)
Parameters
e
PointerEventArgsThe event args.
OnPointerPressed(PointerPressedEventArgs)
Called before the PointerPressed event occurs.
protected override void OnPointerPressed(PointerPressedEventArgs e)
Parameters
e
PointerPressedEventArgsThe event args.
OnPointerReleased(PointerReleasedEventArgs)
Called before the PointerReleased event occurs.
protected override void OnPointerReleased(PointerReleasedEventArgs e)
Parameters
e
PointerReleasedEventArgsThe event args.
OnPropertyChanged(AvaloniaPropertyChangedEventArgs)
Called when a avalonia property changes on the object.
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
Parameters
change
AvaloniaPropertyChangedEventArgsThe property change details.
OnTextInput(TextInputEventArgs)
Called before the TextInput event occurs.
protected override void OnTextInput(TextInputEventArgs e)
Parameters
e
TextInputEventArgsThe 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
intThe 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
AvaloniaPropertyThe property.
state
BindingValueTypeThe current data binding state.
error
ExceptionThe current data binding error, if any.
Events
CopyingToClipboard
Raised when content is being copied to the clipboard
public event EventHandler<RoutedEventArgs>? CopyingToClipboard
Event Type
CuttingToClipboard
Raised when content is being cut to the clipboard
public event EventHandler<RoutedEventArgs>? CuttingToClipboard
Event Type
PastingFromClipboard
Raised when content is being pasted from the clipboard
public event EventHandler<RoutedEventArgs>? PastingFromClipboard
Event Type
TextChanged
Occurs asynchronously after text changes and the new text is rendered.
public event EventHandler<TextChangedEventArgs>? TextChanged
Event Type
TextChanging
Occurs synchronously when text starts to change but before it is rendered.
public event EventHandler<TextChangingEventArgs>? TextChanging
Event Type
Remarks
This event occurs just after the Text property value has been updated.