Partager via


DataGrid.AutoGenerateColumns Property

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

Gets or sets a value that indicates whether columns are created automatically when the ItemsSource property is set.

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

Syntax

'Declaration
Public Property AutoGenerateColumns As Boolean
public bool AutoGenerateColumns { get; set; }
<sdk:DataGrid AutoGenerateColumns="bool"/>

Property Value

Type: System.Boolean
true if columns are generated automatically; otherwise, false. The default is true.

Remarks

Dependency property identifier field: AutoGenerateColumnsProperty

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 set AutoGenerateColumns to false.

Examples

The following code example demonstrates how to set the AutoGenerateColumns property and handle the AutoGeneratingColumn event.

Public Sub New()
    InitializeComponent()
    DG.DataContext = Me.LayoutRoot.Children
End Sub

Private Sub DG_AutoGeneratingColumn(ByVal sender As Object, ByVal e As DataGridAutoGeneratingColumnEventArgs)
    Dim headername As String = e.Column.Header.ToString()

    Select Case headername
        ' Cancel the columns you don't want to generate.
        Case "Effect", "Clip", "Projection", "OpacityMask", "RenderTransformOrigin"
            e.Cancel = True
            Exit Select

            ' Update column headers when generating.
        Case "Opacity"

            e.Column.Header = "Opacity Value"
            Exit Select

        Case "UseLayoutRounding"

            e.Column.Header = "Layout Rounding?"
            Exit Select

        Case "IsHitTestVisible"

            e.Column.Header = "Hit Test Visible?"
            Exit Select

        Case "RenderSize"

            e.Column.Header = "Rendered Size"
            Exit Select

    End Select
End Sub
public MainPage()
{
    InitializeComponent();
    DG.DataContext = this.LayoutRoot.Children;
}

private void DG_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    string headername = e.Column.Header.ToString();

    switch (headername)
    {
        // Cancel the columns you don't want to generate.
        case "Effect":
        case "Clip":
        case "Projection":
        case "OpacityMask":
        case "RenderTransformOrigin":
            e.Cancel = true;
            break;

        // Update column headers when generating.
        case "Opacity":

            e.Column.Header = "Opacity Value";
            break;

        case "UseLayoutRounding":

            e.Column.Header = "Layout Rounding?";
            break;

        case "IsHitTestVisible":

            e.Column.Header = "Hit Test Visible?";
            break;

        case "RenderSize":

            e.Column.Header = "Rendered Size";
            break;
    }

}
    <Grid x:Name="LayoutRoot" Background="White">
        <sdk:DataGrid AutoGenerateColumns="True" ItemsSource="{Binding}" 
        x:Name="DG" AutoGeneratingColumn="DG_AutoGeneratingColumn" />

    </Grid>

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.