Partager via


DataGrid.Columns Property

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Gets a collection that contains all the columns in the control.

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

Syntax

'Declaration
Public ReadOnly Property Columns As ObservableCollection(Of DataGridColumn)
public ObservableCollection<DataGridColumn> Columns { get; }
<sdk:DataGrid>
  <sdk:DataGrid.Columns>
    oneOrMoreColumns
  </sdk:DataGrid.Columns>
</sdk:DataGrid>

XAML Values

Property Value

Type: System.Collections.ObjectModel.ObservableCollection<DataGridColumn>
The columns in the control.

Remarks

Use the Columns collection to add columns, remove columns, or update properties on the columns.

Each column in the Columns collection defines a column in the DataGrid. Columns in the collection must derive from DataGridColumn. DataGridBoundColumn, which adds support for binding, derives from DataGridColumn and is the base for the defined column types.

The following table lists the two defined column types that the DataGrid provides.

Column type

Data type

DataGridTextColumn

Used to display text.

DataGridCheckBoxColumn

Used to display Boolean data.

In addition, you can define your own custom column by using DataGridTemplateColumn .

You can modify the Columns collection at run time regardless of whether it contains generated columns. However, if you specify columns in XAML, you should not set AutoGenerateColumns to true.

You cannot add a column to the Columns collection of more than one DataGrid. If you try to do this, an InvalidOperationException will be thrown.

If you try to modify the Columns collection while the DataGrid is in editing mode, the control will automatically commit the edit. If the control cannot commit the edit, it will throw an InvalidOperationException.

Examples

The following code example demonstrates how to populate the Columns collection in XAML. This example is part of a larger example available in the DataGrid class overview.

<sdk:DataGrid x:Name="dataGrid4" 
    Height="160" Margin="0,5,0,10" 
    RowHeight="40" AutoGenerateColumns="False" >    
    <sdk:DataGrid.Columns>
        <sdk:DataGridTextColumn 
            Header="First Name" 
            Width="SizeToHeader"
            Binding="{Binding FirstName}" 
            FontSize="20" />
        <sdk:DataGridTextColumn 
            Header="Last Name" 
            Width="SizeToCells"
            Binding="{Binding LastName}" 
            FontSize="20" />
        <sdk:DataGridTextColumn 
            Header="Address"
            Width="150"
            Binding="{Binding Address}" >
            <sdk:DataGridTextColumn.ElementStyle>
                <Style TargetType="TextBlock">
                    <Setter Property="TextWrapping" Value="Wrap"/>
                </Style>
            </sdk:DataGridTextColumn.ElementStyle>
            <sdk:DataGridTextColumn.EditingElementStyle>
                <Style TargetType="TextBox">
                    <Setter Property="Foreground" Value="Blue"/>
                </Style>
            </sdk:DataGridTextColumn.EditingElementStyle>
        </sdk:DataGridTextColumn>
        <sdk:DataGridCheckBoxColumn 
            Header="New?" 
            Width="40"
            Binding="{Binding IsNew}" />
        <sdk:DataGridCheckBoxColumn 
            Header="Subscribed?" 
            Width="Auto"
            Binding="{Binding IsSubscribed}" 
            IsThreeState="True" />
    </sdk:DataGrid.Columns>
</sdk:DataGrid>

Version Information

Silverlight

Supported in: 5, 4, 3

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.