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)
Visual Basic (Declaration)
Public ReadOnly Property Columns As ObservableCollection(Of DataGridColumn)
Get
Dim instance As DataGrid
Dim value As ObservableCollection(Of DataGridColumn)
value = instance.Columns
public ObservableCollection<DataGridColumn> Columns { get; }
XAML Property Element Usage
<data:DataGrid>
<data:DataGrid.Columns>
oneOrMoreColumns
</data:DataGrid.Columns>
</data:DataGrid>
XAML Values
- data:
A prefix that is defined to map the XML namespace for the System.Windows.Controls.Data assembly and the System.Windows.Controls CLR namespace.
- oneOrMoreColumns
One or more object elements for concrete classes that derive from DataGridColumn. Typically these are one of the following Silverlight classes: DataGridTemplateColumn, DataGridCheckBoxColumn, DataGridTextColumn.
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.
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.
<data:DataGrid x:Name="dataGrid4"
Height="160" Margin="0,5,0,10"
RowHeight="40" AutoGenerateColumns="False" >
<data:DataGrid.Columns>
<data:DataGridTextColumn
Header="First Name"
Width="SizeToHeader"
Binding="{Binding FirstName}"
FontSize="20" />
<data:DataGridTextColumn
Header="Last Name"
Width="SizeToCells"
Binding="{Binding LastName}"
FontSize="20" />
<data:DataGridTextColumn
Header="Address"
Width="150"
Binding="{Binding Address}" >
<data:DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextWrapping" Value="Wrap"/>
</Style>
</data:DataGridTextColumn.ElementStyle>
<data:DataGridTextColumn.EditingElementStyle>
<Style TargetType="TextBox">
<Setter Property="Foreground" Value="Blue"/>
</Style>
</data:DataGridTextColumn.EditingElementStyle>
</data:DataGridTextColumn>
<data:DataGridCheckBoxColumn
Header="New?"
Width="40"
Binding="{Binding IsNew}" />
<data:DataGridCheckBoxColumn
Header="Subscribed?"
Width="Auto"
Binding="{Binding IsSubscribed}"
IsThreeState="True" />
</data:DataGrid.Columns>
</data:DataGrid>
For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.
Reference
Other Resources