Removing a child control makes parent null

njsokalski

Well-known member
Joined
Mar 16, 2011
Messages
102
Programming Experience
5-10
I currently have the following code in my app:

System.Diagnostics.Debug.WriteLine(String.Format("Me.Parent: {0}", Me.Parent Is Nothing))
CType(Me.Parent, StackPanel).Children.Remove(Me)
System.Diagnostics.Debug.WriteLine(String.Format("Me.Parent: {0}", Me.Parent Is Nothing))

Which gives the following output:

Me.Parent: False
Me.Parent: True

If I comment out the Remove statement, I get

Me.Parent: False
Me.Parent: False

Which obviously means that removing one of the children is causing the parent to become Nothing. Why is removing from a Children collection making the parent Nothing? Any help would be appreciated. Thanks.
 
Parent property is the link from child to parent, Children collection is the link from parent to all its childs. Since these properties reflect the same state updating one must necessarily affect the other, if not the link would be broken.
 
Back
Top