{"record":{"id":"1282bc30d7f70350","repo":"dotnet/wpf","slug":"sr-panel-nonullvisualparent","errorCode":null,"errorMessage":"SR.Panel_NoNullVisualParent","messagePattern":"SR\\.Panel_NoNullVisualParent","errorType":"exception","errorClass":"ArgumentNullException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/UIElementCollection.cs","lineNumber":36,"sourceCode":"    /// the collection from a different context than that of the owning Panel.\n    /// </remarks>\n    /// <seealso cref=\"System.Windows.Media.VisualCollection\" />\n    public class UIElementCollection : IList\n    {\n        /// <summary>\n        ///     The colleciton is the children collection of the visualParent. The logicalParent \n        ///     is used to do logical parenting. The flags is used to invalidate \n        ///     the resource properties in the child tree, if an Application object exists. \n        /// </summary>\n        /// <param name=\"visualParent\">The element of whom this is a children collection</param>\n        /// <param name=\"logicalParent\">The logicalParent of the elements of this collection. \n        /// if overriding Panel.CreateUIElementCollection, pass the logicalParent parameter of that method here.\n        /// </param>\n        public UIElementCollection(UIElement visualParent, FrameworkElement logicalParent)\n        {\n            if (visualParent == null)\n            {\n                throw new ArgumentNullException(SR.Format(SR.Panel_NoNullVisualParent, \"visualParent\", this.GetType()));\n            }\n\n            _visualChildren = new VisualCollection(visualParent);\n            _visualParent = visualParent;\n            _logicalParent = logicalParent;\n        }\n\n\n        /// <summary>\n        /// Gets the number of elements in the collection.\n        /// </summary>\n        public virtual int Count\n        {\n            get { return _visualChildren.Count; }\n        }\n\n        /// <summary>\n        /// Gets a value indicating whether access to the ICollection is synchronized (thread-safe).","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/UIElementCollection.cs#L18-L54","documentation":"The UIElementCollection constructor requires a non-null visualParent; passing null throws ArgumentNullException with SR.Panel_NoNullVisualParent. The visual parent is essential because the collection immediately creates a VisualCollection(visualParent) and every child must have a visual host.","triggerScenarios":"Calling new UIElementCollection(null, someLogicalParent) directly, or overriding Panel.CreateUIElementCollection / Children and returning a collection constructed with a null visualParent.","commonSituations":"Custom panels overriding CreateUIElementCollection during early construction when fields are not yet initialized; unit tests constructing the collection in isolation with a null parent.","solutions":["Pass the owning panel/visual as visualParent (in CreateUIElementCollection pass `this`).","Defer collection creation until the panel is fully constructed.","Null-check the parent before constructing the collection."],"exampleFix":"// before\nreturn new UIElementCollection(null, this);\n// after\nreturn new UIElementCollection(this, this);","handlingStrategy":"validation","validationCode":"if (visualParent == null) throw new InvalidOperationException(\"UIElementCollection requires a visual parent\");\nvar col = new UIElementCollection(visualParent, logicalParent);","typeGuard":null,"tryCatchPattern":"try { var col = new UIElementCollection(parent, parent as FrameworkElement); }\ncatch (ArgumentNullException) { /* parent was null; construct after panel initialization */ }","preventionTips":["In CreateUIElementCollection overrides pass `this` as visualParent.","Construct the collection only after the panel is initialized.","Never build UIElementCollection in unit tests with a null parent."],"tags":["wpf","argument-null","panel","uielementcollection"],"backgroundTag":"null-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}