WPF
ItemsSource
UI Development
Data Binding
Error Handling

WPF "Items collection must be empty before using ItemsSource" fix

Codemia Knowledge Hub ·

WPF throws Items collection must be empty before using ItemsSource when a control such as ListBox, ComboBox or ItemsControl is given items in two ways at once. A control can either hold explicit children in its Items collection or be bound through ItemsSource, never both. Remove the hardcoded items if the control should be data-bound, or drop ItemsSource if the list is static.

Items and ItemsSource are mutually exclusive

WPF item controls support two approaches:

  • add items directly in XAML or code through the Items collection
  • bind the control to a source collection through ItemsSource

This is invalid, because the control has a hardcoded item and also binds ItemsSource:

xml
<ListBox ItemsSource="{Binding Users}">
    <ListBoxItem Content="Hardcoded item" />
</ListBox>

WPF cannot merge the two, so it throws.

Bind with ItemsSource only

If the control shows data from your view model, remove the declared items and bind only through ItemsSource:

xml
<ListBox ItemsSource="{Binding Users}" />
csharp
1using System.Collections.ObjectModel;
2
3public class MainViewModel
4{
5    public ObservableCollection<string> Users { get; } =
6        new ObservableCollection<string> { "Alice", "Bob", "Cara" };
7}

This is the right model for dynamic and MVVM-style lists: the control is a view over the bound collection. In MVVM, let that collection own the item set completely and avoid manipulating items in the view.

Use Items only for static lists

If the list is fixed and needs no binding, do not set ItemsSource:

xml
1<ComboBox>
2    <ComboBoxItem Content="Low" />
3    <ComboBoxItem Content="Medium" />
4    <ComboBoxItem Content="High" />
5</ComboBox>

That is fine for small static menus or demo screens.

Clear Items before assigning ItemsSource in code

The error also appears when code adds items manually and later assigns ItemsSource:

csharp
1var listBox = new ListBox();
2listBox.Items.Add("Manual item");
3
4listBox.ItemsSource = new[] { "Alice", "Bob" }; // throws

If you really need to switch to a bound source, clear the items first:

csharp
1var listBox = new ListBox();
2listBox.Items.Add("Manual item");
3
4listBox.Items.Clear();
5listBox.ItemsSource = new[] { "Alice", "Bob" };

In well-structured WPF code you normally pick one mode and keep it.

Look for hidden child items in XAML

The cause is not always an obvious ListBoxItem. Check for:

  • child elements in the control's XAML that you forgot about
  • a custom control template that injects item children
  • placeholder items left in XAML copied from an example

If ItemsSource is meant to be the source of truth, remove every direct item declaration.

Customize display with ItemTemplate, not child elements

A frequent mistake is adding child items to get custom rendering. To control how each bound item looks, use ItemTemplate:

xml
1<ListBox ItemsSource="{Binding Users}">
2    <ListBox.ItemTemplate>
3        <DataTemplate>
4            <TextBlock Text="{Binding}" />
5        </DataTemplate>
6    </ListBox.ItemTemplate>
7</ListBox>

The data still comes from ItemsSource; the template only controls presentation and does not replace the data source.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms