GroupBox
Controls must be arranged somehow. With a GroupBox
in WPF, we can place them within a box that has a header. This makes interfaces easier to understand and interact with.
In a GroupBox
we can nest another control. We can use the Grid
, Label and CheckBox
controls within a GroupBox
—more details on these sub-controls are available.
The Grid
can be used inside a GroupBox
to lay out other controls. In this example, I dragged a GroupBox
to the Window from the Toolbox in Visual Studio.
GroupBox
, also by dragging, to expand to fit the containing Window.Grid
. I added controls to the Grid
. I moved things around to make them look less awful.GroupBox
control's contribution is the header, which I changed to read "Pizza," and the border around its sub-controls.<Window x:Class="WpfApplication9.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <GroupBox Header="Pizza" HorizontalAlignment="Left" Margin="10,10,0,0" VerticalAlignment="Top" Height="236" Width="263"> <Grid HorizontalAlignment="Left" Height="226" VerticalAlignment="Top" Width="253" Margin="0,0,-2,-12"> <Label Content="These are some pizza toppings." HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0"/> <CheckBox Content="Cheese" HorizontalAlignment="Left" Margin="10,41,0,0" VerticalAlignment="Top"/> <CheckBox Content="Mushrooms" HorizontalAlignment="Left" Margin="10,61,0,0" VerticalAlignment="Top"/> <CheckBox Content="Olives" HorizontalAlignment="Left" Margin="10,81,0,0" VerticalAlignment="Top"/> </Grid> </GroupBox> </Grid> </Window>
A GroupBox
is a container control. We add controls inside of it, which may be more interactive with the user. A Grid
or TabControl
are good choices here.
GroupBox
in our C# code, but such operations are possible.Arranging elements in complex user interfaces is difficult. With a control like GroupBox
we arrange our controls neatly, making better programs.