ListBox
The ListBox
is an ItemsControl
which displays items in a multi-line list box and allows individual selection.
The items to display in the ListBox
are specified using the Items
property. This property will
often be bound to a collection on the control's DataContext
:
<ListBox Items="{Binding MyItems}"/>
Customizing the item display
You can customize how an item is displayed by specifying an ItemTemplate
. For example to display
each item inside a red border with rounded corners:
<ListBox Items="{Binding MyItems}">
<ListBox.ItemTemplate>
<DataTemplate>
<Border Background="Red" CornerRadius="4" Padding="4">
<TextBlock Text="{Binding}"/>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Preventing Horizontal Scrolling
By default if an item is too wide to display in the ListBox
, a horizontal scrollbar will be
displayed. If instead you want items to be constrained to the width of the ListBox
(for example
if you want wrapping text in the items) you can disable the horizontal scrollbar by setting
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
.
<ListBox Items="{Binding MyItems}" Width="250" ScrollViewer.HorizontalScrollBarVisibility="Disabled">
<ListBox.ItemTemplate>
<DataTemplate>
<Border Background="Red" CornerRadius="4" Padding="4">
<TextBlock Text="{Binding}" TextWrapping="Wrap"/>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Customizing the container
Whatever data is displayed in a ListBox
is wrapped in a ListBoxItem
. This control implements
displaying the selection state for individual items in the ListBox
.
Sometimes you may need to modify the style of the ListBoxItem
. In WPF this is done using the
ItemContainerStyle
property, but Avalonia doesn't have this property. Instead add a style for
ListBoxItem
as follows:
<ListBox>
<ListBox.Styles>
<Style Selector="ListBoxItem">
<!-- setters here -->
</Style>
</ListBox.Styles>
</ListBox>