Is it possible to add Controls via a loop to a Control? The difficulty is the name: it is the same in each circulation, so I get only one Control named hu and not 10. How can I modify the code to get 10 Borders?
for (int i = 0; i < 10; i++)
{
Border hu = new Border();
hu.Width = 10;
hu.Height = 10;
hu.Margin = new Thickness(10 * i);
hu.Background = (Brush) typeof(Brushes).GetProperty(Felder[i]).GetValue(null, null);
mastergrid.Children.Add(hu);
}
At first, yes it is possible. What you can do is to set the Name like this
Maybe not in the case of border, but for example a button. How can i get
the data that is responsible for the control in an event, because you bind
one event to several controls. Usually use the Control.Tag Property to add additional
data and use them in a event. For example:
Update
Be aware that the name of the variable that holds your instance, in your case “hu”, is NOT the same as the
NameProperty of your Control.