WPF
ItemsSource
UI Development
Data Binding
Error Handling

Items collection must be empty before using ItemsSource.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In WPF, the error Items collection must be empty before using ItemsSource means the control is being asked to use two different item population models at the same time. A control such as ListBox, ComboBox, or ItemsControl can either contain explicit child items in its Items collection or be bound through ItemsSource, but not both simultaneously.

Why the Error Happens

WPF item controls support two mutually exclusive approaches:

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

The error appears when both are used together. For example, this is invalid because the control contains hardcoded items and also binds ItemsSource.

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

WPF cannot merge those two worlds automatically, so it throws.

Use ItemsSource for Data Binding

If the control is supposed to display data from your view model, remove the manually 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 correct model for dynamic or MVVM-style lists. The control becomes a view over the bound collection instead of a place where you manually curate child items.

Use Items Only for Static Content

If the list is fixed and you do not need data binding, then do not set ItemsSource at all.

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

This is fine for small static menus or demo screens. The problem starts only when a bound source and explicit items are mixed.

Clear Existing Items Before Switching Modes in Code

Sometimes the error happens because code first 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 from manual items to a bound source, clear the existing items first.

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

That said, in well-structured WPF code you usually choose one mode and stay with it rather than switching back and forth.

Watch for Hidden Items in XAML Templates

The error is not always caused by an obvious hardcoded ListBoxItem. It can also happen when:

  • a control has child elements in XAML you forgot about
  • a custom control template injects item children unexpectedly
  • copied XAML includes placeholder items from an example

When debugging, inspect the control markup carefully and remove any direct item declarations if ItemsSource is the intended source of truth.

Use ItemTemplate Instead of Child Elements for Display

A common source of confusion is wanting custom rendering and mistakenly adding child items manually. If you want to control how each bound item looks, use ItemTemplate, not explicit child elements.

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

This keeps the item data coming from ItemsSource while still letting you control presentation.

Common Pitfalls

The most common mistake is leaving one or two hardcoded items in XAML while adding ItemsSource later during data binding. Another is mixing Items.Add(...) in code-behind with a bound collection from the view model.

Developers also sometimes confuse ItemTemplate with item population. Templates control how bound data is displayed; they do not replace the data source itself.

Finally, if you are using MVVM, avoid manual item manipulation in the view. Let the bound collection own the item set completely.

Summary

  • A WPF items control must use either Items or ItemsSource, not both.
  • Remove hardcoded child items if the control is supposed to be data-bound.
  • Use Items.Clear() before assigning ItemsSource only if you are intentionally switching modes.
  • Use ItemTemplate to customize display while still binding through ItemsSource.
  • In MVVM-style code, prefer a bound collection and avoid manual item insertion in the view.

Related reading
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
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.