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(Canvas2DHandl开发者_如何学JAVAer, 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.)
精彩评论