Class Grid
Arranges its Children into a grid of rows and columns defined by
RowDefinitions/ColumnDefinitions, placed via the Row/Column
attached properties (GetRow(UIElement)/SetRow(UIElement, int), GetColumn(UIElement)/SetColumn(UIElement, int)).
[AttachedProperty("GetRow", "SetRow", PropertyName = "Row")]
[AttachedProperty("GetColumn", "SetColumn", PropertyName = "Column")]
[AttachedProperty("GetRowSpan", "SetRowSpan", PropertyName = "RowSpan")]
[AttachedProperty("GetColumnSpan", "SetColumnSpan", PropertyName = "ColumnSpan")]
public class Grid : Panel, INotifyPropertyChanging, IBindingTarget, INotifyPropertyChanged, INotifyFocusChanged, IContainerElement, IContainerLayout
- Inheritance
-
Grid
- Implements
- Inherited Members
- Extension Methods
Remarks
A child with no explicit Row/Column defaults to cell (0, 0). A child whose assigned row/column
index is out of range is clamped into the nearest valid track. A child spans RowSpan×ColumnSpan
cells (GetRowSpan(UIElement), GetColumnSpan(UIElement)), clamped to the tracks that exist.
Constructors
Grid()
Initializes a new instance of the Grid class.
public Grid()
Properties
ColumnDefinitions
Gets the grid's column definitions. An empty collection behaves as a single implicit Star column.
public ObservableCollection<ColumnDefinition> ColumnDefinitions { get; }
Property Value
RowDefinitions
Gets the grid's row definitions. An empty collection behaves as a single implicit Star row.
public ObservableCollection<RowDefinition> RowDefinitions { get; }
Property Value
Methods
ArrangeContent()
Handles custom arrange logic when overridden in any UIElement instance.
protected override void ArrangeContent()
Remarks
This method is called during the arrange pass to position and size the element's content within its final bounds. The element's position and size are already determined by the layout system and available in the ActualBounds property.
When implementing this method:
- Position child elements within the available space;
- Respect the element's padding when positioning content;
- Handle any content-specific layout requirements;
- Call Arrange() on child elements if needed.
GetColumn(UIElement)
Gets the zero-based column element is placed in within its parent Grid.
public static int GetColumn(UIElement element)
Parameters
elementUIElementThe element to get the attached value for.
Returns
- int
The element's assigned column index. Defaults to
0.
GetColumnSpan(UIElement)
Gets how many columns element spans within its parent Grid.
public static int GetColumnSpan(UIElement element)
Parameters
elementUIElementThe element to get the attached value for.
Returns
- int
The column span, at least
1. Defaults to1.
GetRow(UIElement)
Gets the zero-based row element is placed in within its parent Grid.
public static int GetRow(UIElement element)
Parameters
elementUIElementThe element to get the attached value for.
Returns
- int
The element's assigned row index. Defaults to
0.
GetRowSpan(UIElement)
Gets how many rows element spans within its parent Grid.
public static int GetRowSpan(UIElement element)
Parameters
elementUIElementThe element to get the attached value for.
Returns
- int
The row span, at least
1. Defaults to1.
MeasureContent()
Handles custom measure logic when overridden in any UIElement instance.
protected override Size MeasureContent()
Returns
Remarks
This method is called during the measure pass to determine the desired size of the element. The returned size should represent the natural size of the element's content, without considering any constraints, margins, or padding.
When implementing this method:
- Return the natural size needed to display the content;
- Do not include padding, margins, or borders in the returned size;
- Consider the content's actual dimensions (e.g., text length, image size);
- Return Empty if the element has no content.
SetColumn(UIElement, int)
Sets the zero-based column element should be placed in within its parent Grid.
public static void SetColumn(UIElement element, int value)
Parameters
SetColumnSpan(UIElement, int)
Sets how many columns element spans within its parent Grid.
public static void SetColumnSpan(UIElement element, int value)
Parameters
elementUIElementThe element to set the attached value for.
valueintThe column span. Values below
1are treated as1; a span past the last column is clamped.
SetRow(UIElement, int)
Sets the zero-based row element should be placed in within its parent Grid.
public static void SetRow(UIElement element, int value)
Parameters
SetRowSpan(UIElement, int)
Sets how many rows element spans within its parent Grid.
public static void SetRowSpan(UIElement element, int value)