Work around for issue "Element is already the child of another element"?

Apr 5, 2011 at 5:19 PM

I ran into this runtime error "Element is already the child of another element" when changing an object (rectangle) to a usercontrol. The rectangle has a PhysicsObjectBehaviour which seems to be causing this problem. when i remove the behavior everything is working fine.

In the Issue tracker of the Physics helper i fould out that this problem is already logged:

Is there any workaround i could use now? cause this issue is really blocking me.


Apr 7, 2011 at 3:05 PM

It can be a little fussy when creating a user control with physics - you have to have the PhysicsObjectBehavior nested correctly under a Canvas element.

If you take a look at the Demo in the source code download, you can take a look at ucBlock, and try to mimic that structure:

<UserControl {…}>
   <Canvas x:Name="LayoutRoot" Width="60" Height="60">
       <Canvas x:Name="cnvBlock" CacheMode="BitmapCache" Height="60" Width="60">

                    <pb:PhysicsObjectBehavior BoundaryElement="rectBlock" MomentOfIntertia="90000000"/>

             <Rectangle x:Name="rectBlock" Height="60" Stroke="Black" Width="60">
                          <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                                 <GradientStop Color="#FF6786EE" Offset="0.004"/>
                                 <GradientStop Color="#FF0B2478" Offset="1"/>