Reputation: 4928
public partial class Window2 : Window
{
int margin = 200;
public Window2()
{
this.InitializeComponent();
for (int i = 1; i <= 5; i++)
{
TextBlock DynamicLine = new TextBlock();
DynamicLine.Name = "lbl_DynamicLine" + i;
DynamicLine.Width = 600;
DynamicLine.Height = 20;
DynamicLine.Text =i+"Dynamic TextBlock";
DynamicLine.Margin = new Thickness(50, margin, 0, 0);
margin = margin + 20;
LayoutRoot.Children.Add(DynamicLine);
}
}
}
I tried to remove the textblock dynamically like below.
LayoutRoot.Children.Remove(DynamicLine);
But i can remove the last created textblock only with above code line.Now i want to remove all textblock dynamically. what should i do for that.
Upvotes: 2
Views: 2652
Reputation: 2026
try this code
public partial class Window2 : Window
{
int margin = 200;
TextBlock DynamicLine;
public Window2()
{
this.InitializeComponent();
for (int i = 1; i <= 5; i++)
{
DynamicLine = new TextBlock();
DynamicLine.Name = "lbl_DynamicLine" + i;
RegisterName(DynamicLine.Name, DynamicLine);
DynamicLine.Width = 600;
DynamicLine.Height = 20;
DynamicLine.Text =i+"Dynamic TextBlock";
DynamicLine.Margin = new Thickness(50, margin, 0, 0);
margin = margin + 20;
LayoutRoot.Children.Add(DynamicLine);
}
for (int i = 1; i <= 5; i++)
{
DynamicLine = (TextBlock)this.FindName("lbl_DynamicLine" + i);
LayoutRoot.Children.Remove(DynamicLine);
}
}
}
Upvotes: 2
Reputation: 1083
To delete all children, you should call the clear method.
LayoutRoot.Children.Clear();
Upvotes: 1
Reputation: 4205
for (int i = LayoutRoot.Children.Count; i > 0; i--)
{
LayoutRoot.Children.RemoveAt(i-1);
}
Upvotes: 0