Search code examples
wpfcontrolswrappanel

How can I remove a control (e.g. group box) in WPF programmatically?


I am using a WrapPanel and because I want to save space I want to remove (not hide) some controls (some of them are group boxes) depending on some features.

What command should I use to remove a control?


Solution

  • You can use Remove() method or its variant, such as RemoveRange() & RemoveAt(), to remove control(s) from a panel :

    myWrapPanel.Children.Remove(myGroupBox);
    

    Or simply set Visibility property to Collapsed on controls that you want to hide :

    myGroupBox.Visibility = Visibility.Collpased;
    

    That won't cause empty space reserved for those hidden controls as described in MSDN :

    Visibility.Collapsed: Do not display the element, and do not reserve space for it in layout.