I have tried to search google to get the answer but I seem not to be able to ask the question the right way so none of answers describe my problem.
The problem is: I want to create unknown number of controls (grids, textboxes, label {pick one} - it doesn't matter for this example. I'll just edit it for my purpose later) based on result from Object method that returns List<string>
. Let's say the method returns List with 4 items so on startup of the application I want to see four Labels/Textboxes (in rows) with the text from the list.
I'm learning with WPF so I did some tutorials etc and I am able to bind values from object to single Label/Textbox/Control in general but have no idea how to do that dynamically with whole result of List items.
I just can't even imagine that in head so its hard to change it into code.
Let's say I have the following object:
namespace Test
{
public class Robots
{
public List<string> GetAllRobots()
{
List<string> resultList = new List<string>();
resultList.add["Robot1"];
resultList.add["Robot2"];
resultList.add["Robot3"];
resultList.add["Robot4"];
return resultList;
}
}
}
This is the part I have no clue how to build to generate/bind Four separate labels into XAML.
namespace Test
{
/// <summary>
/// Interaction logic for UCRobots.xaml
/// </summary>
public partial class UCRobots : UserControl
{
public UCRobots()
{
InitializeComponent();
List<string> dataList = new Robots().GetAllRobots();
this.DataContext = dataList;
}
}
}
Is there any tutorial how to write the XAML part you could point me to? Or anyone willing to help me in answers?