dotnet/wpf · error · ArgumentNullException
SR.Panel_NoNullVisualParent
Error message
SR.Panel_NoNullVisualParent
What it means
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.
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.
Example fix
// before return new UIElementCollection(null, this); // after return new UIElementCollection(this, this);
Defensive patterns
Strategy: validation
Validate before calling
if (visualParent == null) throw new InvalidOperationException("UIElementCollection requires a visual parent");
var col = new UIElementCollection(visualParent, logicalParent); Try / catch
try { var col = new UIElementCollection(parent, parent as FrameworkElement); }
catch (ArgumentNullException) { /* parent was null; construct after panel initialization */ } Prevention
- 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.
When it happens
Trigger: Calling new UIElementCollection(null, someLogicalParent) directly, or overriding Panel.CreateUIElementCollection / Children and returning a collection constructed with a null visualParent.
Common situations: Custom panels overriding CreateUIElementCollection during early construction when fields are not yet initialized; unit tests constructing the collection in isolation with a null parent.
Related errors
- SR.Panel_NoNullChildren
- A panel with IsItemsHost="true" is not nested in an…
- anchorLocator.Parts
- ArgumentNullException (buffer/sourceBuffer was IntPtr.Zero)
- ArgumentNullException: handle
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/1282bc30d7f70350.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/UIElementCollection.cs:36
/// the collection from a different context than that of the owning Panel.
/// </remarks>
/// <seealso cref="System.Windows.Media.VisualCollection" />
public class UIElementCollection : IList
{
/// <summary>
/// The colleciton is the children collection of the visualParent. The logicalParent
/// is used to do logical parenting. The flags is used to invalidate
/// the resource properties in the child tree, if an Application object exists.
/// </summary>
/// <param name="visualParent">The element of whom this is a children collection</param>
/// <param name="logicalParent">The logicalParent of the elements of this collection.
/// if overriding Panel.CreateUIElementCollection, pass the logicalParent parameter of that method here.
/// </param>
public UIElementCollection(UIElement visualParent, FrameworkElement logicalParent)
{
if (visualParent == null)
{
throw new ArgumentNullException(SR.Format(SR.Panel_NoNullVisualParent, "visualParent", this.GetType()));
}
_visualChildren = new VisualCollection(visualParent);
_visualParent = visualParent;
_logicalParent = logicalParent;
}
/// <summary>
/// Gets the number of elements in the collection.
/// </summary>
public virtual int Count
{
get { return _visualChildren.Count; }
}
/// <summary>
/// Gets a value indicating whether access to the ICollection is synchronized (thread-safe).View on GitHub (pinned to 81131a70a4)