This documentation is archived and is not being maintained.

Grid::ColumnDefinitions Property

Gets a ColumnDefinitionCollection defined on this instance of Grid.

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)

property ColumnDefinitionCollection^ ColumnDefinitions {
	ColumnDefinitionCollection^ get ();

XAML Values


One or more ColumnDefinition elements. Each such ColumnDefinition becomes a placeholder representing a column in the final grid layout.

This example shows how to use the methods in the ColumnDefinitionCollection and RowDefinitionCollection classes to perform actions like adding, clearing, or counting the contents of rows or columns. For example, you can Add, Clear, or Count the items that are included in a ColumnDefinition or RowDefinition. To view the complete sample, see Row and Column Collections Sample.

The following example creates a Grid element with a Name of grid1. The Grid contains a StackPanel that holds Button elements, each controlled by a different collection method. When you click a Button, it activates a method call in the code-behind file.

        <Grid DockPanel.Dock="Top" HorizontalAlignment="Left" Name="grid1" ShowGridLines="true" Width="625" Height="400" Background="#b0e0e6">

        <StackPanel HorizontalAlignment="Left" Orientation="Horizontal" Width="625" DockPanel.Dock="Top">
            <Button Width="125" Click="addCol">Add Column</Button>
            <Button Width="125" Click="addRow">Add Row</Button>
            <Button Width="125" Click="clearCol">Clear All Columns</Button>
            <Button Width="125" Click="clearRow">Clear All Rows</Button>
            <Button Width="125" Click="removeCol">Remove One Column</Button>
        <StackPanel HorizontalAlignment="Left" Orientation="Horizontal" Width="625" DockPanel.Dock="Top">
            <Button Width="125" Click="removeRow">Remove One Row</Button>
            <Button Width="125" Click="colCount">How Many Columns?</Button>
            <Button Width="125" Click="rowCount">How Many Rows?</Button>
            <Button Width="125" Click="rem5Col">Remove 5 Columns</Button>
            <Button Width="125" Click="rem5Row">Remove 5 Rows</Button>			
        <StackPanel HorizontalAlignment="Left" Orientation="Horizontal" Width="625" DockPanel.Dock="Top">
            <Button Width="125" Click="containsRow">Contains Row?</Button>
            <Button Width="125" Click="containsCol">Contains Column?</Button>
            <Button Width="125" Click="insertRowAt">Insert Row</Button>
            <Button Width="125" Click="insertColAt">Insert Column</Button>
            <Button Width="125" Click="colReadOnly">IsReadOnly?</Button>

This example defines a series of custom methods, each corresponding to a Click event in the Extensible Application Markup Language (XAML) file. You can change the number of columns and rows in the Grid in several ways, which includes adding or removing rows and columns; and counting the total number of rows and columns. To prevent ArgumentOutOfRangeException and ArgumentException exceptions, you can use the error-checking functionality that the RemoveRange method provides.

No code example is currently available or this language may not be supported.

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0