Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove elements from stackpanel

Tags:

c#

wpf

stackpanel

I have a button, when pressed it adds a textbox and a listbox to a stackpanel and adds this stackpanel to another stackpanel named "stackPanelAdd". Just like this:

private void buttonAdd_Click(object sender, RoutedEventArgs e)
{
    StackPanel sp = new StackPanel();
    TextBox tb = new TextBox();   
    ListBox lb = new ListBox();

    tb.Margin = new Thickness(5, 5, 5, 0);
    lb.Margin = new Thickness(5, 5, 5, 0);
    lb.Height = 200;

    sp.Children.Add(tb);
    sp.Children.Add(lb);

    stackPanelAdd.Children.Add(sp);
}

How do I remove the last children in the stackpanel "stackPanelAdd"? Should I use something like stackPanelAdd.children.Remove? if so then how do i get the last element in the stackpanel?

like image 417
Phu Minh Pham Avatar asked Oct 22 '25 23:10

Phu Minh Pham


1 Answers

Try:

if (stackPanelAdd.Children.Count>0)
{
  stackPanelAdd.Children.RemoveAt(stackPanelAdd.Children.Count-1);
}
like image 100
Milan Halada Avatar answered Oct 25 '25 22:10

Milan Halada



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!