{"record":{"id":"83a4d77220b42f2b","repo":"dotnet/wpf","slug":"sr-itemcollectionhasnocollection","errorCode":null,"errorMessage":"SR.ItemCollectionHasNoCollection","messagePattern":"SR\\.ItemCollectionHasNoCollection","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemCollection.cs","lineNumber":335,"sourceCode":"\n        /// <summary>\n        /// Retrieve item at the given zero-based index in this CollectionView.\n        /// </summary>\n        /// <remarks>\n        /// <p>The index is evaluated with any SortDescriptions or Filter being set on this CollectionView.</p>\n        /// </remarks>\n        /// <exception cref=\"ArgumentOutOfRangeException\">\n        /// Thrown if index is out of range\n        /// </exception>\n        public override object GetItemAt(int index)\n        {\n            // only check lower bound because Count could be expensive\n            ArgumentOutOfRangeException.ThrowIfNegative(index);\n\n            VerifyRefreshNotDeferred();\n\n            if (!EnsureCollectionView())\n                throw new InvalidOperationException(SR.ItemCollectionHasNoCollection);\n\n            if (_collectionView == _internalView)\n            {\n                // check upper bound here because we know it's not expensive\n                ArgumentOutOfRangeException.ThrowIfGreaterThanOrEqual(index, _internalView.Count);\n            }\n\n            return _collectionView.GetItemAt(index);\n        }\n\n\n\n        /// <summary>\n        ///     Insert an item in the collection at a given index.  All items\n        /// after the given position are moved down by one.\n        /// </summary>\n        /// <param name=\"insertIndex\">\n        ///     The index at which to inser the item","sourceCodeStart":317,"sourceCodeEnd":353,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/ItemCollection.cs#L317-L353","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nvar item = listBox.Items.GetItemAt(0);\n// after\nif (listBox.Items.Count > 0)\n    var item = listBox.Items.GetItemAt(0);","handlingStrategy":"validation","validationCode":"if (itemsControl.ItemsSource != null && itemsControl.Items.Count > index)\n    var item = itemsControl.Items.GetItemAt(index);","typeGuard":"static bool HasItems(ItemCollection c) => c != null && c.Count > 0;","tryCatchPattern":"try { var item = items.GetItemAt(i); }\ncatch (InvalidOperationException) { item = null; /* no collection bound */ }","preventionTips":["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"],"tags":["wpf","itemcollection","invalidoperationexception","binding"],"backgroundTag":"empty-result-set","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}