WPF GroupBox


The GroupBox is a familiar control for organizing numerous controls. You might use it to surround a group of CheckBoxes or radio buttons. The GroupBox is a content control, and as a content control, it can only have one child. You need to set a GroupBox’s content to an intermediate control that can contain multiple children. For example, you might use a StackPanel.

The Header property of the GroupBox can be set to an arbitrary object. If that object derives from UIElement, it is rendered as expected. For example, the header could be a fully-functional Button.

1<Window x:Class="GroupBoxSimple.MainWindow"
6        xmlns:local="clr-namespace:GroupBoxSimple"
7        mc:Ignorable="d"
8        WindowStartupLocation="CenterScreen"
9        Title="GroupBoxSimple" Height="200" Width="300">
10    <Grid>
11        <GroupBox Margin="10">
12            <GroupBox.Header>
13                <Button Click="Button_Click">To Do</Button>
14            </GroupBox.Header>
15            <StackPanel>
16                <CheckBox>Shovel the Snow</CheckBox>
17                <CheckBox>Cut the grass</CheckBox>
18                <CheckBox>Take out the garbage</CheckBox>
19            </StackPanel>
20        </GroupBox>
21    </Grid>
22</Window>

Here is some code behind.

1using System.Windows;
2 
3namespace GroupBoxSimple
4{
5    public partial class MainWindow : Window
6    {
7        public MainWindow()
8        {
9            InitializeComponent();
10        }
11        private void Button_Click(object sender, RoutedEventArgs e)
12        {
13            MessageBox.Show("Some work to do.", "GroupBoxSimple",
14                MessageBoxButton.OKCancel, MessageBoxImage.Warning);
15        }
16    }
17}