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. </Label>
  9. <ListBox Margin="30,61,519,183" Height="131">
  10. <ListBoxItem IsSelected="true">Mahesh Chand</ListBoxItem>
  11. <ListBoxItem>Allen O'neill</ListBoxItem>
  12. <ListBoxItem>Dave McCarter</ListBoxItem>
  13. <ListBoxItem>Monica Rathbun</ListBoxItem>
  14. <ListBoxItem>John Morehouse</ListBoxItem>
  15. </ListBox>
  16. <Button Margin="30,213,519,127" Content="Selected Author"/>
  17. </Grid>
  18. </GroupBox>
The new Window looks like the following.
WPF GroupBox with controls
In this article, we saw how to use a WPF GroupBox.