WPF GroupBox ExampleUse the WPF GroupBox, which combines a header with a box around other controls. Nest a Grid within GroupBox.
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 used 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.
Then I adjusted the bounds of the GroupBox, also by dragging, to expand to fit the containing Window.
Next I added a Grid. I added controls to the Grid. I moved things around to make them look less awful.
So The GroupBox control's contribution is the header, which I changed to read "Pizza," and the border around its sub-controls.
Title="MainWindow" Height="350" Width="525">
="Pizza" HorizontalAlignment="Left" Margin="10,10,0,0"
VerticalAlignment="Top" Height="236" Width="263">
<Grid HorizontalAlignment="Left" Height="226" VerticalAlignment="Top"
<Label Content="These are some pizza toppings."
HorizontalAlignment="Left" VerticalAlignment="Top" Margin="10,10,0,0"/>
<CheckBox Content="Cheese" HorizontalAlignment="Left"
<CheckBox Content="Mushrooms" HorizontalAlignment="Left"
<CheckBox Content="Olives" HorizontalAlignment="Left"
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.
Note We do not need to manipulate the GroupBox in our C# code, but such operations are possible.
Tip In WPF, any property can be changed in code. If needed, we do not need to use Visual Studio to manually change the XAML.
A summary. Arranging elements in complex user interfaces is difficult. With a control like GroupBox we arrange our controls neatly, making better programs.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on 9/29/2022 (edit).