An ItemsControl in which individual items can be selected.
Namespace
Inheritance
Control → TemplatedControl → ItemsControl → SelectingItemsControl → ListBox
Properties
Gets or sets the selected items. Use this property for multiple selection scenarios.
Gets or sets the selection model that manages the selection state.
Gets or sets the selection mode. Values: Single (default), Multiple, Toggle, AlwaysSelected.
Gets the scroll information for the ListBox. Read-only.
Methods
Selects all items in the ListBox.
Deselects all items in the ListBox.
Template Parts
The scroll viewer that contains the items panel.
Usage
XAML Example - Basic ListBox
<ListBox ItemsSource="{Binding Items}"
SelectedItem="{Binding SelectedItem}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Name}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
XAML Example - Static Items
<ListBox SelectionMode="Single">
<ListBoxItem Content="Item 1" />
<ListBoxItem Content="Item 2" />
<ListBoxItem Content="Item 3" />
<ListBoxItem Content="Item 4" IsEnabled="False" />
<ListBoxItem Content="Item 5" />
</ListBox>
XAML Example - Multiple Selection
<ListBox ItemsSource="{Binding Products}"
SelectionMode="Multiple"
SelectedItems="{Binding SelectedProducts}">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" Spacing="10">
<TextBlock Text="{Binding Name}" FontWeight="Bold" />
<TextBlock Text="{Binding Price, StringFormat='{}{0:C}'}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
C# Example - Programmatic Selection
var listBox = new ListBox
{
ItemsSource = new ObservableCollection<string>
{
"Apple", "Banana", "Cherry", "Date", "Elderberry"
}
};
// Single selection
listBox.SelectedIndex = 2; // Selects "Cherry"
// Access selected item
var selected = listBox.SelectedItem as string;
C# Example - Multiple Selection
var listBox = new ListBox
{
SelectionMode = SelectionMode.Multiple,
ItemsSource = items
};
// Select multiple items
listBox.Selection.Select(0);
listBox.Selection.Select(2);
listBox.Selection.Select(4);
// Or select all
listBox.SelectAll();
// Access selected items
var selectedItems = listBox.SelectedItems;
foreach (var item in selectedItems)
{
Debug.WriteLine(item);
}
// Clear selection
listBox.UnselectAll();
ViewModel Example
public class MainViewModel : ViewModelBase
{
public ObservableCollection<Person> People { get; }
private Person? _selectedPerson;
public Person? SelectedPerson
{
get => _selectedPerson;
set => this.RaiseAndSetIfChanged(ref _selectedPerson, value);
}
private IList? _selectedPeople;
public IList? SelectedPeople
{
get => _selectedPeople;
set => this.RaiseAndSetIfChanged(ref _selectedPeople, value);
}
public MainViewModel()
{
People = new ObservableCollection<Person>
{
new Person { Name = "Alice", Age = 30 },
new Person { Name = "Bob", Age = 25 },
new Person { Name = "Charlie", Age = 35 }
};
}
}
Custom Item Container Style
<ListBox ItemsSource="{Binding Items}">
<ListBox.Styles>
<Style Selector="ListBoxItem">
<Setter Property="Padding" Value="10" />
<Setter Property="Margin" Value="2" />
</Style>
<Style Selector="ListBoxItem:selected">
<Setter Property="Background" Value="#2196F3" />
<Setter Property="Foreground" Value="White" />
</Style>
</ListBox.Styles>
</ListBox>
Virtualized ListBox
<ListBox ItemsSource="{Binding LargeDataSet}"
VirtualizationMode="Simple"
Height="400">
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<VirtualizingStackPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
</ListBox>
SelectionMode Enum
Only a single item can be selected at once.
Multiple items can be selected using Ctrl+Click or Shift+Click.
Items toggle selection on click without requiring modifier keys.
At least one item must always be selected.
Source File
ListBox.cs:1-160
See Also