简体   繁体   中英

wpf: remove control in GroupBox

I have a GroupBox inside a Canvas containing different controls. At runtime I want to change the GroupBox to an Expander.

No, I cannot do that in xaml. That would be easy!

I get all the children of the GroupBox and know that I have to detache the children from it to add it to a new Visual.

Here's the code:

 for (int i= 0; i < VisualTreeHelper.GetChildrenCount(Canvas2DHandler); i++)
                 {
                     DependencyObject child = VisualTreeHelper.GetChild(Canvas2DHandler, i);

                     if (child != null && typeof(GroupBox) == child.GetType() )
                     {
                         GroupBox roomGroupBox = (GroupBox)child;
                         Expander roomExpander = new Expander();
                         StackPanel sPForExpander = new StackPanel();
                         roomExpander.Header = roomGroupBox.Header;
                         for (int n=0; n < VisualTreeHelper.GetChildrenCount(child); n++)
                         {
                             UIElement groupBoxChild = VisualTreeHelper.GetChild(child, n) as UIElement;
                             //remove control from groupBox (HOWTO???)

                             sPForExpander.Children.Add(groupBoxChild);
                         }
                         roomExpander.Content = sPForExpander;

                     }
                 }

While I'm trying to get information about removing the children I cannot even find a definition for children of a GroupBox. Might that be the point?

How can I solve this / is there completely other way?

tanks for HELP!

Stef

GroupBox has only one child - look at Content property, so you have to remove controls from it.

Use:
((TypeOfContainer)roomGroupBox.Content).Children.Remove(groupBoxChild);

Replace "TypeOfContainer" with type of panel within your groupbox (StackPanel etc.)

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM