Creating Basic UI Elements
Avalonia UI provides a rich set of controls and containers for building user interfaces. In this section, we'll explore the fundamental UI elements and how to use them in your applications.
Controls and Containers
Avalonia UI organizes its UI elements into two main categories:
- Controls: Interactive elements that users can interact with (buttons, text boxes, etc.)
- Containers: Elements that organize and arrange other elements (panels, grids, etc.)
Control Hierarchy
All visual elements in Avalonia UI inherit from a common hierarchy:
AvaloniaObject
: Base class for all Avalonia objectsVisual
: Base class for all visual elementsControl
: Base class for all controls- Specific controls (Button, TextBox, etc.)
Types of Controls
Avalonia UI controls can be classified into several types:
- Drawn Controls: Generate their own geometry or bitmaps (Border, TextBlock, Image)
- Layout Controls: Define the layout of their children (Grid, StackPanel)
- Templated Controls: Controls whose visual appearance is defined in a XAML control template (Button, TextBox)
Creating Controls in XAML
To create a control in XAML, you simply add the corresponding element to your XAML file:
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
x:Class="MyApp.MainWindow"
Title="My Avalonia App">
<Button Content="Click Me" />
</Window>
Creating Controls in Code
You can also create controls programmatically in C#:
// Create a new button programmatically
var button = new Button
{
Content = "Click Me",
HorizontalAlignment = Avalonia.Layout.HorizontalAlignment.Center,
VerticalAlignment = Avalonia.Layout.VerticalAlignment.Center,
Margin = new Avalonia.Thickness(10)
};
// Add the button to a container (assuming myPanel is a Panel control)
myPanel.Children.Add(button);
// Optionally, attach an event handler to the button
button.Click += (sender, e) =>
{
// Handle the button click event
var clickedButton = (Button)sender;
clickedButton.Content = "Clicked!";
};
Text and Input Controls
Text and input controls allow users to view and enter text in your application.
TextBlock
TextBlock
is used to display read-only text:
<TextBlock Text="Hello, Avalonia UI!"
FontSize="24"
FontWeight="Bold"
Foreground="#1E88E5"
HorizontalAlignment="Center"
VerticalAlignment="Center"
TextAlignment="Center"
TextWrapping="Wrap"
Margin="20"
Padding="10"
Background="#E3F2FD"
CornerRadius="4"
Opacity="0.9"
ToolTip.Tip="This is a TextBlock example" />
You can also use inline formatting for rich text:
<TextBlock TextWrapping="Wrap" LineHeight="24" Margin="10">
<Run Text="TextBlock supports " />
<Run Text="bold" FontWeight="Bold" />
<Run Text=", " />
<Run Text="italic" FontStyle="Italic" />
<Run Text=", and " />
<Run Text="colored" Foreground="#E91E63" />
<Run Text=" text. You can also add " />
<LineBreak />
<Run Text="line breaks and " />
<Run Text="different " FontSize="18" />
<Run Text="font " FontSize="22" />
<Run Text="sizes" FontSize="26" />
<Run Text=" within the same TextBlock." />
</TextBlock>
For more complex text formatting, you can use Span
elements:
<TextBlock Margin="10">
<TextBlock.Inlines>
<Span>
You can use <Bold>Bold</Bold>, <Italic>Italic</Italic>, and
<Span Foreground="Red">colored</Span> text with Span elements.
</Span>
<LineBreak />
<Span FontFamily="Courier New">
Different font families are also supported.
</Span>
<LineBreak />
<Span TextDecorations="Underline">Underlined text</Span>
<Span> and </Span>
<Span TextDecorations="Strikethrough">strikethrough</Span>
<Span> are available too.</Span>
</TextBlock.Inlines>
</TextBlock>
TextBox
TextBox
allows users to enter and edit text:
<TextBox Text="{Binding UserInput}"
Watermark="Enter text here..."
Width="250"
Height="30"
CornerRadius="4"
BorderThickness="1"
BorderBrush="#BDBDBD"
Padding="8,4"
FontSize="14"
VerticalContentAlignment="Center"
HorizontalAlignment="Left"
Margin="10"
MaxLength="50"
ToolTip.Tip="Enter your text here" />
For a multi-line text box:
<TextBox AcceptsReturn="True"
TextWrapping="Wrap"
Height="100"
Width="300"
Watermark="Enter multiple lines of text..."
ScrollViewer.VerticalScrollBarVisibility="Auto"
FontFamily="Consolas"
Padding="8"
Margin="10" />
For a password field:
<TextBox PasswordChar="•"
Watermark="Enter password..."
Width="250"
RevealPassword="False"
Classes="password"
Margin="10" />
Common properties:
Text
: The text content (often bound to a view model property)Watermark
: Placeholder text shown when the TextBox is emptyIsReadOnly
: Makes the TextBox read-onlyAcceptsReturn
: Allows multi-line input with Enter keyPasswordChar
: Character to display instead of actual text (for password fields)MaxLength
: Maximum number of characters allowedTextWrapping
: Controls whether text wraps to a new lineSelectionStart
/SelectionEnd
: Control text selectionCaretIndex
: Controls the position of the text cursorRevealPassword
: When true, shows the password text in a password field
NumericUpDown
NumericUpDown
is used for numeric input with increment/decrement buttons:
<NumericUpDown Value="5"
Minimum="0"
Maximum="10"
Increment="0.5"
FormatString="F1" />