views:

161

answers:

1

I have a grid/canvas that has an element (say an icon with an image and text overlayed) added to it dynamically via code.

Most of the time it renders correctly when added (content is aligned properly inside it), but sometimes all the content sits in one corner.

I can remove/add/remove/add/remove/add and it will eventually do it

After a movement of the parent canvas, the element corrects itself as if the layout has been updated.

I have tried the following on Loaded and OnApplyTemplate for both the element and the element parent but it still seems to happen occasionally

  • InvalidateMeasure()
  • InvalidateArrange()
  • UpdateLayout()

Any ideas on why the content wouldn't arrange would be appreciated cause its driving me nuts

+1  A: 

I'm not sure why it works for you sometimes but not others. But a grid has the inherent ability to dynamically resize itself and its contents. A canvas doesn't (you'd have to handle that manually in code).

If you change your canvas to a grid does it work? A grid has slightly more overhead than a canvas but it sounds to me like you need its functionality anyway.

Steve Wortham
Still does it. I changed it again to make sure.
burnt_hand
Annoyingly enough, the issue was a grid, but a grid higher in the tree than the one I was adding. It was changed to a canvas and no problems now.
burnt_hand