ListBox
is an ItemsControl
which displays items in a multi-line list box and allows individual selection.ListBox
are specified using the Items
property. This property will often be bound to a collection on the control's DataContext
:ItemTemplate
. For example to display each item inside a red border with rounded corners:ListBox
will be wrapped in a ListBoxItem
- this is called the container. The container hosts the content specified in the ItemTemplate
but it is not part of the ItemTemplate
itself. It is the container that contains the logic for displaying selected items.ListBoxItem
in the ListBox
:ItemContainerStyle
- this property does not exist in Avalonia; you should use the method outlined above.ListBox
:SelectedIndex
, SelectedItem
, SelectedItems
or Selection
properties.ListBox
:Single
Multiple
Toggle
AlwaysSelected
SelectionMode
is set to Single
.Items
collection, or in the case of multiple selection the first selected item. You will often want to bind this to a view model if your list SelectionMode
is set to Single
.Items
collection contains duplicates as it is impossible to distinguish between duplicate values.Selection
property exposes an ISelectionModel
object with various methods to track multiple selected items. You can create a SelectionModel
object in your view model and bind it to this property and subsequently control the selection from your view model.ISelectionModel
is optimized for large collections. Because of this it is recommended that you use this property in preference to SelectedItems
for performance reasons.Selection
is bound to a SelectionModel
, SelectedItems
will no longer function.SelectionModel
also exposes batching functionality through its Update()
method and a SelectionChanged
event which details exactly which items have been selected and deselected.IList
. It can be bound to any list that implements IList
but it will usually be bound to a collection which also implements INotifyCollectionChanged
such as ObservableCollection<>
.SelectedItems
can be very poor, particularly on large collections. It is recommended that you use the Selection
property instead.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"
.