TextEditor
The TextEditor
control provides base text editing features. It supports masks that allow you to restrict user input and to format values.
The control's main features include:
- Support for single-line and multi-line text.
- Text wrapping.
- Using masks to specify a pattern for data input.
- Using masks to format values when the editor does not have focus.
- Text selection.
- Watermarks
Specify Text and Customize Text Options
The TextEditor.EditorValue
property specifies the editor's text. If the text contains NewLine characters, the editor displays the text on multiple lines.
Text Wrapping
The TextEditor.TextWrapping
property allows you to activate automatic text wrapping at the editor's right edge. Set this property to the Avalonia.Media.TextWrapping.Wrap
value to enable regular text wrapping mode.
Accept Tab and Enter keys During Input
Users can press the Tab and Enter keys to insert Tab and Return characters in the text. You can use the following options to change this behavior:
TextEditor.AcceptsReturn
— Specifies whether a NewLine character is inserted when the Enter key is pressed. If this property is disabled, the editor ignores the Enter key.TextEditor.AcceptsTab
— Specifies whether a tab character is inserted when the Tab key is pressed. If this property is disabled, focus is moved to the next control in the tab order when the Tab key is pressed.
Masks
A mask applied to a text editor guides a user to enter values according to a specified pattern. Masked input is enabled for the DateEditor and SpinEditor controls, and disabled for other text editors, by default.
To enable masked input, do the following:
- Set the
TextEditor.MaskType
property to a mask type. - Set the
TextEditor.Mask
property to a mask.
Mask Type
The TextEditor.MaskType
property allows you to select mask mode from the following options:
MaskType.Numeric
— Mask mode tailored to accept numeric values.MaskType.DateTime
— Mask mode tailored to accept date-time values.MaskType.None
— Masked input is disabled.
Mask Strings
Use the TextEditor.Mask
property to set a string that specifies a mask. The mask is composed of mask specifiers which are specific to the selected mask type.
See the following topics for more information:
Example - Allow input of integer values in a Text Editor
The following code applies the "d" numeric mask to restrict data input to integer values in a TextEditor.
xmlns:mxe="https://schemas.eremexcontrols.net/avalonia/editors"
<mxe:TextEditor Name="textEditor1" MaskType="Numeric" Mask="d"
HorizontalContentAlignment="Right"/>
Using the Mask as a Display Format
The mask specified by the TextEditor.Mask
property can be used to format the editor's value in display mode (when text editing is not active). The TextEditor.MaskUseAsDisplayFormat
property specifies this setting. The property's default value is true
.
Mask Culture Settings
Many masks are dependent on the current culture. For instance, the decimal separator for numeric masks is different in different cultures. Date-time values are formatted using culture-specific patterns and localized names for the days of the week and months.
The TextEditor.MaskCulture
property allows you to set the culture to use by the mask. If this property is not set, the application's default culture is used.
Example
The following code sets the "c" numeric mask to enter currency values in a text editor, and applies the Chinese (Simplified) culture to the mask.
textEditor1.EditorValue = 12.34567;
textEditor1.MaskType = Eremex.AvaloniaUI.Controls.Editors.MaskType.Numeric;
textEditor1.MaskCulture = new CultureInfo("zh-CN");
textEditor1.Mask = "c";
Text Selection
A user can select text using the mouse and keyboard.
To select a portion of text and clear the selection in code, use the following API:
TextEditor.SelectionStart
— The zero-based index of the starting character of the text selection.TextEditor.SelectionEnd
— The zero-based index of the end character of the text selection.TextEditor.SelectAll
— Selects all text.TextEditor.ClearSelection
— Deselects the selected text.
Watermarks
The TextEditor
control and its descendants support watermarks. A watermark is a grayed out hint displayed when the editor's value is empty or null.
Use the TextEditor.Watermark
property to specify a watermark.
xmlns:mxe="https://schemas.eremexcontrols.net/avalonia/editors"
<mxe:TextEditor Name="textEditor1" Watermark="Type to search"/>