TextBox
The TextBox control is an editable text field where a user can input text.

Reference

​TextBox​

Source code

​TextBox.cs​

Examples

Basic one line TextBox

1
<Window xmlns="https://github.com/avaloniaui"
2
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
4
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
6
x:Class="AvaloniaAppTemplate.MainWindow"
7
Title="AvaloniaAppTemplate">
8
<StackPanel Margin="10">
9
<TextBox />
10
</StackPanel>
11
</Window>
Copied!
produces the following output in Windows 10

Password input TextBox

1
<Window xmlns="https://github.com/avaloniaui"
2
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
4
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
6
x:Class="AvaloniaAppTemplate.MainWindow"
7
Title="AvaloniaAppTemplate">
8
<StackPanel Margin="10">
9
<TextBox PasswordChar="#" />
10
</StackPanel>
11
</Window>
Copied!
produces the following output in Windows 10 when text is input

TextBox with watermark

Avalonia can show a "watermark" in a TextBox, which is a piece of text that is displayed when the TextBox is empty (in HTML5 this is called a placeholder)
1
<Window xmlns="https://github.com/avaloniaui"
2
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
4
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
6
x:Class="AvaloniaAppTemplate.MainWindow"
7
Title="AvaloniaAppTemplate">
8
<StackPanel Margin="10">
9
<TextBox Watermark="Street address" />
10
</StackPanel>
11
</Window>
Copied!
produces the following output in Windows 10

Multiline TextBox

1
<Window xmlns="https://github.com/avaloniaui"
2
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
4
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
6
x:Class="AvaloniaAppTemplate.MainWindow"
7
Title="AvaloniaAppTemplate">
8
<Grid Margin="10">
9
<TextBox AcceptsReturn="True" TextWrapping="Wrap" />
10
</Grid>
11
</Window>
Copied!
produces the following output in Windows 10 when text is input

TextInput Event Handling

By default the TextInput event does nothing if you assign directly to it. This is due to the TextBox itself handling the event from the underlying InputElement.
If you wish to access the TextInput event, then you will have to use the TextBox.AddHandler method to intercept the event via event tunneling.
1
MyTextInput.AddHandler(TextInputEvent, MyTextInput_InputHandler, RoutingStrategies.Tunnel);
Copied!
To see more details about this behavior, read routed events documentation page.
Last modified 3mo ago