locked
Grid.Children.Add is not setting the parent property of the added element RRS feed

  • Question

  • Hi,

    I have a grid defined in XAML.

    In C# i am adding FrameworkElement objects to the grid by

    gridObject.Children.Add(elementObject);

    Grid.SetRow() Grid.SetColumn();

    it seems as though the Parent property of the element is not getting set to the gridObject.

    Does anyone know what is casuing this?

    Tuesday, October 29, 2013 11:33 PM

Answers

  • I added a element.UpdateLayout() call in there which is getting the job done.
    • Marked as answer by c.j.mcnaught Wednesday, October 30, 2013 3:59 AM
    Wednesday, October 30, 2013 3:59 AM

All replies

  • Hi C.J.

    There is some asynchronicity going on inside the Add. If you wait a moment the Parent should be set, but it doesn't get set immediately.

    --Rob

    Tuesday, October 29, 2013 11:57 PM
    Moderator
  • Hi Rob,

    So I tried to add

    await Task.Delay(5000);

    directly after i add the element to the Grid.

    In debug mode i can't get it to go past the await line

    What have i done worng?

    Wednesday, October 30, 2013 1:28 AM
  •  I am not sure about the asynchronicity theory, but what I have found during debugging is that:I have gone through and added a heap of element to this grid and upon completion, each of the elements parent property is null

    as soon as i touch one of the ui elements on the page ie. click a button the parent property of all the items is set to the grid.

    Is there a way for force that from happening without having to interact with the ui first?

    Wednesday, October 30, 2013 3:55 AM
  • I added a element.UpdateLayout() call in there which is getting the job done.
    • Marked as answer by c.j.mcnaught Wednesday, October 30, 2013 3:59 AM
    Wednesday, October 30, 2013 3:59 AM