Malcolm
Malcolm

Reputation: 1807

Remove parent of an UIElement in Silverlight

I have an object of an UIElement, how can I remove the parent of it?

I saw that there is not setter for parent property of an UIElement.

Any suggestions will be helpful.

EDIT :

 protected FrameworkElement Content
        {
            get { return this.content; }
            set
            {
                if ((this.content != null) && (this.Children.Contains(this.content)
== true))
                    this.Children.Remove(this.content);
                this.content = value;
                if ((this.content != null) && (this.Children.Contains(this.content)
== false))
                {
                    this.Children.Add(this.content); // here i get error Element is already an child of another
                }

                this.InvalidateMeasure();
                this.InvalidateArrange();
            }
        }

Upvotes: 0

Views: 3241

Answers (1)

gideon
gideon

Reputation: 19465

UIElement.Parent just returns the Parent as a UIElement. You can cast it to the right element if you know what the parent is. Lets say you have a parent this IS a StackPanel

 StackPanel parent = myelement.Parent as StackPanel;
 parent.Children.Remove(myelement);//removes your element from its parent.

Upvotes: 3

Related Questions