0

If I create instances of my UserControls, and then add them to the StackPanel like follows:

var myUC1 = new MyUserControl(GetControlData<Quick_Box>());
var myUC2 = new MyUserControl(GetControlData<Quick_Star>());
myStackPanel.Children.Add(myUC1);
myStackPanel.Children.Add(myUC2);

I can see that they're added, one after the other. However, if I don't create the instances beforehand, and attempt to create the instances as I'm adding them to the StackPanel as follows:

myStackPanel.Children.Add(new MyUserControl(GetControlData<Quick_Box>()));
myStackPanel.Children.Add(new MyUserControl(GetControlData<Quick_Star>()));

the second instance always overwrites the first. Why is this?

DaveDev
  • 41,155
  • 72
  • 223
  • 385
  • Can you post a short but complete program demonstrating the problem?# – Jon Skeet Oct 16 '12 at 13:13
  • 2
    There's no difference functionally between the two pieces of code, unless your `MyUserControl` or `GetControlData` code has something unexplained in it. You say the second instance "overwrites" the first - what do you mean by that? What do you see that leads you to that conclusion? Have you *confirmed* that there is only a single child control (in Snoop or in your debugger), or do you only see one child control when you run the app and have assumed that the other doesn't exist? – Dan Puzey Oct 16 '12 at 13:24

0 Answers0