Export (0) Print
Expand All

ColumnDefinitionCollection Class

Provides access to an ordered, strongly typed collection of ColumnDefinition objects.

Namespace: System.Windows.Controls
Assembly: PresentationFramework (in presentationframework.dll)
XML Namespace:  http://schemas.microsoft.com/winfx/2006/xaml/presentation

public sealed class ColumnDefinitionCollection : IList<ColumnDefinition>, ICollection<ColumnDefinition>, 
	IEnumerable<ColumnDefinition>, IList, ICollection, IEnumerable
public final class ColumnDefinitionCollection implements IList<ColumnDefinition>, 
	ICollection<ColumnDefinition>, IEnumerable<ColumnDefinition>, IList, 
	ICollection, IEnumerable
public final class ColumnDefinitionCollection implements IList<ColumnDefinition>, 
	ICollection<ColumnDefinition>, IEnumerable<ColumnDefinition>, IList, 
	ICollection, IEnumerable
You cannot use this managed class in XAML.

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 Manipulating 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">
  <Grid.ColumnDefinitions>
    <ColumnDefinition/>
    <ColumnDefinition/>
    <ColumnDefinition/>
  </Grid.ColumnDefinitions>
  <Grid.RowDefinitions>
  <RowDefinition/>
    <RowDefinition/>
    <RowDefinition/>
  </Grid.RowDefinitions>
</Grid>

<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>
<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>
<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>
</StackPanel>    

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.

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;

namespace columndefinitions_grid
{
    public partial class Window1 : Window
    {
        RowDefinition rowDef1;
        ColumnDefinition colDef1;

        private void addCol(object sender, RoutedEventArgs e) 
        {
            colDef1 = new ColumnDefinition();
            grid1.ColumnDefinitions.Add(colDef1);
        }

        private void addRow(object sender, RoutedEventArgs e)
        {
            rowDef1 = new RowDefinition();
            grid1.RowDefinitions.Add(rowDef1);
        }

        private void clearCol(object sender, RoutedEventArgs e)
        {
            grid1.ColumnDefinitions.Clear();
        }

        private void clearRow(object sender, RoutedEventArgs e)
        {
            grid1.RowDefinitions.Clear();
        }

        private void removeCol(object sender, RoutedEventArgs e)
        {
            if (grid1.ColumnDefinitions.Count <= 0)
            {
                tp1.Text = "No More Columns to Remove!";
            }
            else
            {
                grid1.ColumnDefinitions.RemoveAt(0);
            }
        }

        private void removeRow(object sender, RoutedEventArgs e)
        {
            if (grid1.RowDefinitions.Count <= 0)
            {
                tp1.Text = "No More Rows to Remove!";
            }
            else
            {
                grid1.RowDefinitions.RemoveAt(0);
            }
        }

        private void colCount(object sender, RoutedEventArgs e)
        {
            tp2.Text = "The current number of Columns is: " + grid1.ColumnDefinitions.Count;
        }

        private void rowCount(object sender, RoutedEventArgs e)
        {
            tp2.Text = "The current number of Rows is: " + grid1.RowDefinitions.Count;
        }

        private void rem5Col(object sender, RoutedEventArgs e)
        {
            if (grid1.ColumnDefinitions.Count < 5)
            {
                tp1.Text = "There aren't five Columns to Remove!";
            }
            else
            {
                grid1.ColumnDefinitions.RemoveRange(0,5);
            }
        }

        private void rem5Row(object sender, RoutedEventArgs e)
        {
            if (grid1.RowDefinitions.Count < 5)
            {
                tp1.Text = "There aren't five Rows to Remove!";
            }
            else
            {
                grid1.RowDefinitions.RemoveRange(0, 5);
            }
        }

        private void containsRow(object sender, RoutedEventArgs e)
        {
            if (grid1.RowDefinitions.Contains(rowDef1))
            {
                tp2.Text = "Grid Contains RowDefinition rowDef1";
            }
            else
            {
                tp2.Text = "Grid Does Not Contain RowDefinition rowDef1";
            }
        }

        private void containsCol(object sender, RoutedEventArgs e)
        {
            if (grid1.ColumnDefinitions.Contains(colDef1))
            {
                tp3.Text = "Grid Contains ColumnDefinition colDef1";
            }
            else
            {
                tp3.Text = "Grid Does Not Contain ColumnDefinition colDef1";
            }
        }

        private void colReadOnly(object sender, RoutedEventArgs e)
        {
            tp4.Text = "RowDefinitionsCollection IsReadOnly?: " + grid1.RowDefinitions.IsReadOnly.ToString();
            tp5.Text = "ColumnDefinitionsCollection IsReadOnly?: " + grid1.ColumnDefinitions.IsReadOnly.ToString();
        }

        private void insertRowAt(object sender, RoutedEventArgs e)
        {
            rowDef1 = new RowDefinition();
            grid1.RowDefinitions.Insert(grid1.RowDefinitions.Count, rowDef1);
            tp1.Text = "RowDefinition added at index position " + grid1.RowDefinitions.IndexOf(rowDef1).ToString();
        }

        private void insertColAt(object sender, RoutedEventArgs e)
        {
            colDef1 = new ColumnDefinition();
            grid1.ColumnDefinitions.Insert(grid1.ColumnDefinitions.Count, colDef1);
            tp2.Text = "ColumnDefinition added at index position " + grid1.ColumnDefinitions.IndexOf(colDef1).ToString();
        }
    }
}

System.Object
  System.Windows.Controls.ColumnDefinitionCollection

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Windows 98, Windows Server 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0

Community Additions

ADD
Show:
© 2014 Microsoft