Am having three textboxes and one textblock control inside a grid in XAML. In for loop I have to repeat the controls based on the given count. Please suggest any idea.
Try This:
for (int i = 0; i <Count; i++)
{
var textBlock = new TextBlock();
textBlock.Name = "txtblock" + i.ToString();
textBlock.Text = "Dynamic Textblock " + i.ToString();
var textBox = new TextBox();
textBox.Name = "txtbox" + i.ToString();
textBox.Text = "Dynamic Textblock " + i.ToString();
GridName.Children.Add(textBlock);
GridName.Children.Add(textBox);
}
You can add Properties as Your Requirements
Why don't you try having those controls within a Data Template
?
So that it'll automatically list according to your data.
<DataTemplate>
<TextBox />
<TextBox />
<TextBox />
<TextBlock />
</DataTemplate>
Or else try this way if you're going to go with a loop.
for (int i =0; i <2; i++)
{
textbox[i]= new TextBox { Text = str[i] } ;
ContentPanel.Children.Add(textbox[i]);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.