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.
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
Itemscollection - 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.
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.
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.
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.
If you really need to switch from manual items to a bound source, clear the existing items first.
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.
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
ItemsorItemsSource, not both. - Remove hardcoded child items if the control is supposed to be data-bound.
- Use
Items.Clear()before assigningItemsSourceonly if you are intentionally switching modes. - Use
ItemTemplateto customize display while still binding throughItemsSource. - In MVVM-style code, prefer a bound collection and avoid manual item insertion in the view.
Related reading
- IUnityContainer.ResolveT throws error claiming it cannot be used with type parameters
- Java Equivalent of C async/await?
- Java equivalent to C extension methods
- Java equivalent to
- itgendid012 Last part of the SQL statement has not been recognized on distributed Exact Online query
- iTunes account creation not allowed when trying to test In-App Purchases
- Java Future vs c async await
- Java System.currentTimeMillis equivalent in C

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack 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.