DataGrid.AutoGeneratingColumn 事件

定义

在自动生成了单独的列时发生。

public:
 event EventHandler<System::Windows::Controls::DataGridAutoGeneratingColumnEventArgs ^> ^ AutoGeneratingColumn;
public event EventHandler<System.Windows.Controls.DataGridAutoGeneratingColumnEventArgs> AutoGeneratingColumn;
member this.AutoGeneratingColumn : EventHandler<System.Windows.Controls.DataGridAutoGeneratingColumnEventArgs> 
Public Custom Event AutoGeneratingColumn As EventHandler(Of DataGridAutoGeneratingColumnEventArgs) 
Public Event AutoGeneratingColumn As EventHandler(Of DataGridAutoGeneratingColumnEventArgs) 

事件类型

示例

以下示例演示如何在事件处理程序中创建 AutoGeneratingColumn 列并更改列。

<!-- The DataGrid.DataContext is a DataTable that contains a list of customers.  The DataTable columns are  
     Title, FirstName, MiddleName, LastName, Suffix, CompanyName, EmailAddress, and Phone.-->
<DataGrid Name="DG1" ItemsSource="{Binding}" AutoGenerateColumns="True" AutoGeneratingColumn="DG1_AutoGeneratingColumn" />
//Access and update columns during autogeneration
private void DG1_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    string headername = e.Column.Header.ToString();

    //Cancel the column you don't want to generate
    if (headername == "MiddleName")
    {
        e.Cancel = true;
    }

    //update column details when generating
    if (headername == "FirstName")
    {
        e.Column.Header = "First Name";
    }
    else if (headername == "LastName")
    {
        e.Column.Header = "Last Name";
    }
    else if (headername == "EmailAddress")
    {
        e.Column.Header = "Email";
    }
}
'Access and update columns during autogeneration
Private Sub DG1_AutoGeneratingColumn(ByVal sender As Object, ByVal e As DataGridAutoGeneratingColumnEventArgs)
    Dim headername As String = e.Column.Header.ToString()
    'Cancel the column you don't want to generate
    If headername = "MiddleName" Then
        e.Cancel = True
    End If

    'update column details when generating
    If headername = "FirstName" Then
        e.Column.Header = "First Name"
    ElseIf headername = "LastName" Then
        e.Column.Header = "Last Name"
    ElseIf headername = "EmailAddress" Then
        e.Column.Header = "Email"
    End If
End Sub

注解

可以更改或取消在事件处理程序中创建的 AutoGeneratingColumn 列。

当所有列都自动生成时,将发生 该 AutoGeneratedColumns 事件。

适用于

另请参阅