views:

28

answers:

1

Hi,

If I add a control to a canvas and than removes it, I can not re-add it to the same canvas (or to any other canvas for that matter) any idea how can I reset the parent?

mainCanvas.Children.Add(item); mainCanvas.Children.Remove(item);

mainCanvas.Children.Add(item); // Will throw an exception that parent was already set.

Thanks, Eden.

A: 

Are you sure there is not something else going on in your code?

I just tried a new wpf application with :

public MainWindow()
    {
        InitializeComponent();

        Button b = new Button();
        b.Content = "hello";
        Canvas c = new Canvas();
        c.Children.Add(b);
        c.Children.Remove(b);
        c.Children.Add(b);
        Content = c;
    }

and it worked fine. Have you got any collection changed delegates?

David Hollinshead
Thanks. I inherited a code base which is a mess... I'll have to dig in.
Eden