views:

156

answers:

1

In the following code, person.Children contains 3 other Person objects. This code adds the child.Loaded event handler to all three, but child_Loaded only executes for the first two. Any idea why this is?

foreach ( Person child in person.Children)
        {
            //Add children in same position as parent
            child.x_PositionTransform.X = person.x_PositionTransform.X;
            child.x_PositionTransform.Y = person.x_PositionTransform.Y;

            child.SetValue(Canvas.NameProperty, "child" + objCount++);
            child.Click += new RoutedEventHandler(person_Click);
            x_LayoutRoot.Children.Add(child);

            child.Loaded += new RoutedEventHandler(child_Loaded);
        }
+3  A: 

I can't say for sure that this is the issue, but I'd try setting the Loaded event handler before adding the child to the x_LayoutRoot object.

Ben M
That fixed it. Thanks a bunch!
oltman