All controls in group help you with adjusting the layout of your application
You can think of it as a container for your controls, which helps you to arrange them in a specific way
How Size of Control is determined
The size of a control is determined by its Width and Height properties, which can be set to a specific value or to Auto
Width = 50 means control will be 50 pixels wide
Width = Auto means control will take as much space as it needs to display its content
Width = * means control will take all available space in the container, and if there are multiple controls with *, they will share the available space equally
You can also use MinWidth, MaxWidth, MinHeight, and MaxHeight properties to restrict the size of a control
Window
Title bar consule around 30-40 pixels of height so adjust your window height accordingly so all your controls are visible
Side border is around 8-10 pixels of width so adjust your window width accordingly so all your controls are visible
You can use WindowStyle property to hide title bar and border of window
Or Can set WindowStyle="None" and ResizeMode="NoResize" to make your window borderless and non-resizable
Grid
This is sample code to create a Grid with 4 rows and 2 columns
You can use Grid.Row and Grid.Column properties to set your control to a specific position
You can also create another Grid inside a Grid cell to create complex layouts
Additionally use can use Grid.RowSpan and Grid.ColumnSpan properties to make your control span across multiple rows or columns without creating extra rows or columns in your grid
| Property | Description |
|----------|-------------|
| Grid.Row | Sets the row index of the control |
| Grid.Column | Sets the column index of the control |
| Grid.RowSpan | Sets the number of rows the control should span |
| Grid.ColumnSpan | Sets the number of columns the control should span |
Stack Panel
Arranges all elements in single line vertically/horizontally
Perfect when you need single direction layouts like list
DockPanel allows you to dock your controls to the top, bottom, left or right of the container
You can use DockPanel.Dock property to set your control to a specific position
If no dock position is specified, the control will assume the default position of Left
You can use it for setting up a header, footer, sidebar, or any other layout where you want to dock controls to the edges of the container
<DockPanel><!-- Top --><TextBlockText="Header"Background="LightBlue"DockPanel.Dock="Top"Padding="10"/><!-- Left --><ButtonContent="Menu"Width="80"DockPanel.Dock="Left"/><!-- Remaining space --><TextBoxText="Main Content Area"AcceptsReturn="True"/></DockPanel>
+-------------------------+
| Header |
+-----+-------------------+
|Menu | |
| | Main Content Area |
| | |
+-----+-------------------+
It's simplest layout control in WPF, Unlike Grid and StackPanel, Canvas does not have any layout logic, it just places the controls at the specified position
You can use Canvas.Left, Canvas.Top, Canvas.Right and Canvas.Bottom properties to set your control to a specific position
You can use it for drawing or visualization scenarios where you need to place controls at specific coordinates
Here is sample code to place button on canvas and move it on click of button
//Code behind for window with canvaspublicpartialclassMainWindow:Window{privatedouble_x=50;publicMainWindow(){InitializeComponent();}privatevoidMyButton_Click(objectsender,RoutedEventArgse){_x+=20;Canvas.SetLeft(MyButton,_x);}}
//Sample code to add rectangle on canvas via code behindRectanglerect=newRectangle{Width=100,Height=50,Fill=Brushes.LightGreen};Canvas.SetLeft(rect,100);Canvas.SetTop(rect,100);MyCanvas.Children.Add(rect);//Sample code to add circle on canvas via code behindEllipsecircle=newEllipse{Width=50,Height=50,Fill=Brushes.LightBlue};Canvas.SetLeft(circle,200);Canvas.SetTop(circle,150);MyCanvas.Children.Add(circle);
Property
Description
Canvas.Left
Sets the distance between the left edge
Canvas.Top
Sets the distance between the top edge
Canvas.Right
Sets the distance between the right edge
Canvas.Bottom
Sets the distance between the bottom edge
Canvas.ZIndex
Set Zvalue of control, higher will be on top
Method
Description
Canvas.SetLeft(element, value)
Set X position
Canvas.SetTop(element, value)
Set Y position
Canvas.GetLeft(element)
Read X position
Canvas.GetTop(element)
Read Y position
Canvas.Children.Add(element)
Add a control or shape
Content Control
It's placeholder that can display a single piece of content which you can swap out at runtime, such as a button, text, image, or entire layout.
You can use it to create dynamic UIs where the content changes based on user interaction or application state like swapping between different views or pages in a single window.
Perfect for sidebar navigation with different views
Content Control is base class for many other controls like Button, Label, ListBox, ComboBox, etc. which can hold a single piece of content.
<ContentControlName="MainContent"/>
//Code behind to set content of ContentControlMainContent.Content=newButton{Content="Click Me"};
Scrollviewer is used to make your content scrollable when it exceeds the available space
You can use it to wrap any content that might overflow, such as long lists, large images, or complex layouts.
Some controls like ListBox, DataGrid, and RichTextBox have built-in scrolling capabilities, but you can use ScrollViewer for custom content or layouts that require scrolling.
| Property | Description |
| ---------|-------------|
| HorizontalScrollBarVisibility | Controls the visibility of the horizontal scrollbar|
| VerticalScrollBarVisibility | Controls the visibility of the vertical scrollbar|
Grid Splitter
GridSplitter is used to resize the grid columns or rows at runtime
You have to add extra row or column in your grid to add GridSplitter control with 5-10 thickness to make it visible and resizable