Search code examples
c#wpftextblock

How to remove textblock dynamically in wpf


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.


Solution

  • 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);
            }
    
        }
    }