0

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();
            }
        }
Dave Clemmer
  • 3,741
  • 12
  • 49
  • 72
Malcolm
  • 1,801
  • 3
  • 21
  • 48
  • can you give an example? Do you want to remove a child element from its parent from within the child code? This usually stumps people. – gideon Mar 26 '11 at 08:18
  • i get an object of an ui element say from somewhere an i want to insert this object in an panel but as that ui element already has parent set , is causing problem for me. – Malcolm Mar 26 '11 at 08:22
  • You want to **insert** the element **into** a panel. Then this element becomes a **child **. You would do `panel.Children.Add(yourelement);` – gideon Mar 26 '11 at 08:24

1 Answers1

3

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.
gideon
  • 19,329
  • 11
  • 72
  • 113