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.

<Window x:Class="GroupBoxSimple.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:GroupBoxSimple"
        mc:Ignorable="d"
        WindowStartupLocation="CenterScreen"
        Title="GroupBoxSimple" Height="200" Width="300">
    <Grid>
        <GroupBox Margin="10">
            <GroupBox.Header>
                <Button Click="Button_Click">To Do</Button>
            </GroupBox.Header>
            <StackPanel>
                <CheckBox>Shovel the Snow</CheckBox>
                <CheckBox>Cut the grass</CheckBox>
                <CheckBox>Take out the garbage</CheckBox>
            </StackPanel>
        </GroupBox>
    </Grid>
</Window>

Here is some code behind.

using System.Windows;

namespace GroupBoxSimple
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Some work to do.", "GroupBoxSimple", 
                MessageBoxButton.OKCancel, MessageBoxImage.Warning);
        }
    }
}