dotnet/wpf · error · InvalidOperationException
SR.ItemCollectionHasNoCollection
Error message
SR.ItemCollectionHasNoCollection
What it means
ItemCollection.GetItemAt throws InvalidOperationException(SR.ItemCollectionHasNoCollection) when ItemsSource has not been set (or the binding produced no collection) and no items were added directly, so the ItemCollection has no underlying collection view to index into. Without a view there is no item at any index.
Solutions
- Set ItemsSource to a non-null collection before calling GetItemAt.
- Check items.Count > 0 (or IsUsingItemsSource / EnsureCollectionView state) before indexing.
- If relying on a binding, defer the call until DataContext is available (e.g. Loaded event) and verify the binding produced a value.
Example fix
// before
var item = listBox.Items.GetItemAt(0);
// after
if (listBox.Items.Count > 0)
var item = listBox.Items.GetItemAt(0); Defensive patterns
Strategy: validation
Validate before calling
if (itemsControl.ItemsSource != null && itemsControl.Items.Count > index)
var item = itemsControl.Items.GetItemAt(index); Type guard
static bool HasItems(ItemCollection c) => c != null && c.Count > 0;
Try / catch
try { var item = items.GetItemAt(i); }
catch (InvalidOperationException) { item = null; /* no collection bound */ } Prevention
- Only call GetItemAt after ItemsSource is bound and Count > 0
- Defer item access until the control's Loaded event
- Verify bindings resolve (no null DataContext) before indexing
When it happens
Trigger: Calling GetItemAt(index) on an ItemsControl whose ItemsSource binding returned null or was never assigned, and which has no direct items added via Items.Add.
Common situations: Accessing listbox.Items.GetItemAt(...) before DataContext/binding resolves; binding to a property that initially returns null; calling in a constructor before the template/binding engine has supplied the source.
Understand the failure class
Background: EmptyResultError / "no results found": when an API or scraper succeeds but returns zero rows — this error's family across 9 libraries.
Related errors
- SR.Format(SR.MemberNotAllowedForView…
- SR.Format(SR.MemberNotAllowedForView, "AddNew")
- SR.Format(SR.MemberNotAllowedForView, "CancelNew")
- SR.Format(SR.MemberNotAllowedForView, "CommitNew")
- SR.Format(SR.MemberNotAllowedForView, "EditItem")
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/83a4d77220b42f2b.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemCollection.cs:335
/// <summary>
/// Retrieve item at the given zero-based index in this CollectionView.
/// </summary>
/// <remarks>
/// <p>The index is evaluated with any SortDescriptions or Filter being set on this CollectionView.</p>
/// </remarks>
/// <exception cref="ArgumentOutOfRangeException">
/// Thrown if index is out of range
/// </exception>
public override object GetItemAt(int index)
{
// only check lower bound because Count could be expensive
ArgumentOutOfRangeException.ThrowIfNegative(index);
VerifyRefreshNotDeferred();
if (!EnsureCollectionView())
throw new InvalidOperationException(SR.ItemCollectionHasNoCollection);
if (_collectionView == _internalView)
{
// check upper bound here because we know it's not expensive
ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, _internalView.Count);
}
return _collectionView.GetItemAt(index);
}
/// <summary>
/// Insert an item in the collection at a given index. All items
/// after the given position are moved down by one.
/// </summary>
/// <param name="insertIndex">
/// The index at which to inser the itemView on GitHub (pinned to 81131a70a4)