-2

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?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
not_loqan
  • 91
  • 1
  • 1
  • 9
  • Your code won't compile. – dymanoid Sep 06 '19 at 15:42
  • It sounds like [this provides the outlines of what you want](https://stackoverflow.com/questions/3063031/itemscontrol-itemtemplate-binding). Change the ItemTemplate as needed. Since your list items are strings, the only bindings in your itemtemplate will just be stuff like ``: The "DataContext" inside the itemtemplate will be for one string from the list. – 15ee8f99-57ff-4f92-890c-b56153 Sep 06 '19 at 15:50
  • ItemsControl is definitely a way to go. You might also want to research what is ObservableCollection, and why use it instead of List in WPF – ASh Sep 06 '19 at 18:00

1 Answers1

2

You can display the content of your collection with any type of ItemsControl. Some examples are ListBox, TreeView, HeaderedItemsControl, TabControl, and even ItemsControl itself. These controls take a list of items and display them according to the ItemTemplate.

In your example, you would edit the UCRobots.xaml file to have the following

<!-- The ItemsSource property defines the list of items. 
     Here we are binding directly to the DataContext of the UCRobots class. 
     You could also bind to a property of the object that is set as the DataContext -->
<ItemsControl ItemsSource="{Binding}"> 
    <!-- 
         The below is commented out because the default DataTemplate for the ItemTemplate 
         property is to display a TextBlock that binds directly to the item
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding}"/>
            The Text property is binding directly to the item in the list. 
            If your list contained objects with properties, you could bind to one of those properties. 
            For example, if your list contained a list of People objects, you could bind the 
            Text property to the Name property of your class
        </DataTemplate>
    </ItemsControl.ItemTemplate>
    -->
</ItemsControl>
Shawn Kendrot
  • 12,425
  • 1
  • 25
  • 41