WPF - Layouts-Grid Panel - Windows Presentation Foundation(WPF)

What is WPF Grid Panel?

A Grid Panel provides a flexible area which consists of rows and columns. In a Grid, child elements can be arranged in tabular form. Elements can be added to any specific row and column by using Grid.Row and Grid.Column properties

By default, a Grid panel is created with one row and one column. Multiple rows and columns are created by RowDefinitions and ColumnDefinitions properties. The height of rows and the width of columns can be defined in the following three ways −

  • Fixed value − To assign a fixed size of logical units (1/96 inch)
  • Auto − It will take space which are required for the controls in that specific row/column.
  • Star (*) − It will take the remaining space when Auto and fixed sized are filled.

The hierarchical inheritance of Canvas class is as follows.

hierarchy_of_gridpanel

Commonly Used Properties of Grid Class

Given below are the most commonly used properties of Grid.

Sr. No. Property & Description
1
Background
Gets or sets a Brush that fills the panel content area. (Inherited from Panel)
2
Children
Gets a UIElementCollection of child elements of this Panel. (Inherited from Panel.)
3
ColumnDefinitions
Gets a list of ColumnDefinition objects defined on this instance of Grid.
4
Height
Gets or sets the suggested height of the element. (Inherited from FrameworkElement.)
5
ItemHeight
Gets or sets a value that specifies the height of all items that are contained within a WrapPanel.
6
ItemWidth
Gets or sets a value that specifies the width of all items that are contained within a WrapPanel.
7
Margin
Gets or sets the outer margin of an element. (Inherited from FrameworkElement.)
8
Name
Gets or sets the identifying name of the element. The name provides a reference so that code-behind, such as event handler code, can refer to a markup element after it is constructed during processing by a XAML processor. (Inherited from FrameworkElement.)
9
Orientation
Gets or sets a value that specifies the dimension in which child content is arranged.
10
Parent
Gets the logical parent element of this element. (Inherited from FrameworkElement.)
11
Resources
Gets or sets the locally-defined resource dictionary. (Inherited from FrameworkElement.)
12
RowDefinitions
Gets a list of RowDefinition objects defined on this instance of Grid.
13
Style
Gets or sets the style used by this element when it is rendered. (Inherited from FrameworkElement.)
14
Width
Gets or sets the width of the element. (Inherited from FrameworkElement.)

Commonly Used Methods of Grid Class

Given below are the most commonly used methods of Grid.

Sr. No. Methods & Description
1
GetColumn
Gets the value of the Grid.Column XAML attached property from the specified FrameworkElement.
2
GetColumnSpan
Gets the value of the Grid.ColumnSpan XAML attached property from the specified FrameworkElement.
3
GetRow
Gets the value of the Grid.Row XAML attached property from the specified FrameworkElement.
4
SetColumn
Sets the value of the Grid.Column XAML attached property on the specified FrameworkElement.
5
SetRow
Sets the value of the Grid.Row XAML attached property on the specified FrameworkElement.
6
SetRowSpan
Sets the value of the Grid.RowSpan XAML attached property on the specified FrameworkElement.

Example

The following example shows how to add child elements into a Grid to specify it in a tabular form. In the following XAML implementation, Text Blocks are added in the first column and Text Boxes are added in the second column of the Grid.

When you compile and execute the above code, it will produce the following window.

output_of_gridpanel

We recommend that you execute the above example code and try some of the other properties of this class as well.

All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Windows Presentation Foundation(WPF) Topics