如何在Silverlight中动态创建多个控件?

时间:2022-11-27 19:39:43

I know how to create a list of Controls and add new instances of them to it:

我知道如何创建一个控件列表并添加它们的新实例:

private List<FirstCircleControl> Circles = new List<FirstCircleControl>();
FirstCircleControl mc = new FirstCircleControl();
Circles.Add(mc);

I want to add a whole bunch of "FirstCircleControls". How would I add 10 controls to my list? I want to be able to "create" and then "add" them to the list using a loop.

我想添加一大堆“FirstCircleControls”。如何在列表中添加10个控件?我希望能够“创建”,然后使用循环将它们“添加”到列表中。

1 个解决方案

#1


I wonder why you might need to create them all at once and then add them to the list, but here's a solution:

我想知道为什么你可能需要一次创建它们然后将它们添加到列表中,但这是一个解决方案:

Enumerable.Range(0, 10)
          .Select(x => new FirstCircleControl())
          .ToList()                        // Forces creation of controls.
          .ForEach(x => Circles.Add(x));   // Adds them to the list.

#1


I wonder why you might need to create them all at once and then add them to the list, but here's a solution:

我想知道为什么你可能需要一次创建它们然后将它们添加到列表中,但这是一个解决方案:

Enumerable.Range(0, 10)
          .Select(x => new FirstCircleControl())
          .ToList()                        // Forces creation of controls.
          .ForEach(x => Circles.Add(x));   // Adds them to the list.