ListBox
Selectable list with keyboard navigation, ObservableCollection binding, and selection indicators.
Overview
The ListBox control extends Selector and supports keyboard navigation,
mouse selection, and two-way SelectedItem binding. Items can be provided via
ItemsSource binding to an ObservableCollection.
ViewModel (C#)
public class ListsViewModel : ViewModelBase
{
public ObservableCollection<string> MenuItems { get; } =
["Dashboard", "Messages", "Settings", "Profile", "Help"];
public string? SelectedMenuItem
{
get;
set
{
if (SetProperty(ref field, value))
SelectionText = value != null ? $"Selected: {value}" : "No selection";
}
}
public ICommand AddItemCommand => field ??= new RelayCommand(() =>
{
MenuItems.Add($"Item {++_counter}");
});
public ICommand RemoveItemCommand => field ??= new RelayCommand(() =>
{
if (SelectedMenuItem != null)
MenuItems.Remove(SelectedMenuItem);
});
}
Key Concepts
ItemsSourcebinding toObservableCollection<T>SelectedItemwithMode=TwoWayfor selection tracking- Live collection updates reflected in the UI automatically
- Custom
UserControlwithx:Classand DependencyProperties - Keyboard navigation (arrow keys, Home/End)