Using WPF GroupBox in C# and XAML

By default, a GroupBox can have one child only but multiple child controls can be added by placing a container control on a GroupBox such as a Grid or StackPanel.
 
The GroupBox element in XAML represents a GroupBox control. The following code snippet creates a GroupBox control, sets its background and font. The code also sets the header by using GroupBox.Header. 
  1. <GroupBox Margin="10,10,10,10" FontSize="16" FontWeight="Bold" Background="LightGray">  
  2. <GroupBox.Header>  
  3. C# Corner  
  4. </GroupBox.Header>  
  5. <TextBlock FontSize="14" FontWeight="Regular">  
  6. This is a group box control content.  
  7. </TextBlock>  
  8. </GroupBox>  
The output looks like the following windows where a group box is displayed with its content.
 
WPF GroupBox  
 
The content of a GroupBox can be any content including multiple child controls. If you need to place multiple controls, you may need to use a container control such as a panel or grid.
 
You can format a GroupBox header’s color and border thickness.
 
The following code example creates a GroupBox with an orange border with multiple controls.
 
  1. <GroupBox Margin="10,10,10,10" FontSize="16" FontWeight="Bold"  
  2.             Background="LightYellow" BorderBrush="Orange" BorderThickness="3">  
  3.             <GroupBox.Header>  
  4.                 Author Information  
  5.             </GroupBox.Header>  
  6.             <Grid>  
  7.                 <Label Margin="20,20,0,0" Content="C# Corner Featured Authors">  
  8.   
  9.                 </Label>  
  10.                 <ListBox Margin="30,61,519,183" Height="131">  
  11.                     <ListBoxItem IsSelected="true">Mahesh Chand</ListBoxItem>  
  12.                     <ListBoxItem>Allen O'neill</ListBoxItem>  
  13.                     <ListBoxItem>Dave McCarter</ListBoxItem>  
  14.                     <ListBoxItem>Monica Rathbun</ListBoxItem>  
  15.                     <ListBoxItem>John Morehouse</ListBoxItem>  
  16.                 </ListBox>  
  17.   
  18.                 <Button Margin="30,213,519,127" Content="Selected Author"/>  
  19.             </Grid>  
  20.         </GroupBox>  
The new Window looks like the following.
 
WPF GroupBox with controls 
 
In this article, we saw how to use a WPF GroupBox. 
 


Recommended Free Ebook
Similar Articles
Mindcracker
Founded in 2003, Mindcracker is the authority in custom software development and innovation. We put best practices into action. We deliver solutions based on consumer and industry analysis.