DataGrid.Columns 属性

定义

获取一个集合,该集合包含 DataGrid 中的所有列。

public:
 property System::Collections::ObjectModel::ObservableCollection<System::Windows::Controls::DataGridColumn ^> ^ Columns { System::Collections::ObjectModel::ObservableCollection<System::Windows::Controls::DataGridColumn ^> ^ get(); };
public System.Collections.ObjectModel.ObservableCollection<System.Windows.Controls.DataGridColumn> Columns { get; }
member this.Columns : System.Collections.ObjectModel.ObservableCollection<System.Windows.Controls.DataGridColumn>
Public ReadOnly Property Columns As ObservableCollection(Of DataGridColumn)

属性值

ObservableCollection<DataGridColumn>

DataGrid 中列的集合。

示例

以下示例演示如何向集合添加列。

<!-- ItemsSource is a DataTable that contains a list of customers.  The DataTable columns are  
     Title, FirstName, MiddleName, LastName, Suffix, CompanyName, EmailAddress, and Phone.-->
<DataGrid Grid.Row="2" Name="DG2" ItemsSource="{Binding}" AutoGenerateColumns="False" />
public Window1()
{
Public Sub New()
    //Create a new column to add to the DataGrid
    DataGridTextColumn textcol = new DataGridTextColumn();
    //Create a Binding object to define the path to the DataGrid.ItemsSource property
    //The column inherits its DataContext from the DataGrid, so you don't set the source
    Binding b = new Binding("LastName");
    //Set the properties on the new column
    textcol.Binding = b;
    textcol.Header = "Last Name";
    //Add the column to the DataGrid
    DG2.Columns.Add(textcol);
}
    'Create a new column to add to the DataGrid
    Dim textcol As New DataGridTextColumn()
    'Create a Binding object to define the path to the DataGrid.ItemsSource property 
    'The column inherits its DataContext from the DataGrid, so you don't set the source
    Dim b As New Binding("LastName")
    'Set the properties on the new column
    textcol.Binding = b
    textcol.Header = "Last Name"
    'Add the column to the DataGrid

    DG2.Columns.Add(textcol)
End Sub

以下示例演示如何从集合中删除列。

<Button Content="Delete First Column" Click="Button_Click" />
<!-- ItemsSource is a DataTable that contains a list of customers.  The DataTable columns are  
     Title, FirstName, MiddleName, LastName, Suffix, CompanyName, EmailAddress, and Phone.-->
<DataGrid Grid.Row="1" Name="DG1" ItemsSource="{Binding}" AutoGeneratingColumn="DG1_AutoGeneratingColumn"  />
private void Button_Click(object sender, RoutedEventArgs e)
{
    //Delete the first column whether visible or not
    DG1.Columns.RemoveAt(0);
}
Private Sub Button_Click(ByVal sender As Object, ByVal e As RoutedEventArgs)
    'Delete the first column whether visible or not
    DG1.Columns.RemoveAt(0)
End Sub

以下示例演示如何在自动生成列和事件发生时对集合中的列设置属性。

<CheckBox Content="View Customer Details" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked" />
<!-- ItemsSource is a DataTable that contains a list of customers.  The DataTable columns are  
     Title, FirstName, MiddleName, LastName, Suffix, CompanyName, EmailAddress, and Phone.-->
<DataGrid Grid.Row="1" Name="DG1" ItemsSource="{Binding}" AutoGeneratingColumn="DG1_AutoGeneratingColumn"  />
private void DG1_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    //Set properties on the columns during auto-generation
    switch (e.Column.Header.ToString())
    {
        case "LastName":
            e.Column.CanUserSort = false;
            e.Column.Visibility = Visibility.Visible;
            break;
        case "FirstName":
            e.Column.Visibility = Visibility.Visible;
            break;
        case "CompanyName":
            e.Column.Visibility = Visibility.Visible;
            break;
        case "EmailAddress":
            e.Column.Visibility = Visibility.Visible;
            break;
        default:
            e.Column.Visibility = Visibility.Collapsed;
            break;
    }
}

private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
    //Make each column in the collection visible
    foreach (DataGridColumn col in DG1.Columns)
    {
        col.Visibility = Visibility.Visible;
    }
}

private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
    //Get the columns collection
    ObservableCollection<DataGridColumn> columns = DG1.Columns;

    //set the visibility for each column so only 4 columns are visible
    foreach (DataGridColumn col in columns)
    {
        switch (col.Header.ToString())
        {
            case "LastName":
                col.Visibility = Visibility.Visible;
                break;
            case "FirstName":
                col.Visibility = Visibility.Visible;
                break;
            case "CompanyName":
                col.Visibility = Visibility.Visible;
                break;
            case "EmailAddress":
                col.Visibility = Visibility.Visible;
                break;
            default:
                col.Visibility = Visibility.Collapsed;
                break;
        }
    }
}
Private Sub DG1_AutoGeneratingColumn(ByVal sender As Object, ByVal e As DataGridAutoGeneratingColumnEventArgs)
    'Set properties on the columns during auto-generation
    Select Case e.Column.Header.ToString()
        Case "LastName"
            e.Column.CanUserSort = False
            e.Column.Visibility = Visibility.Visible
            Exit Select
        Case "FirstName"
            e.Column.Visibility = Visibility.Visible
            Exit Select
        Case "CompanyName"
            e.Column.Visibility = Visibility.Visible
            Exit Select
        Case "EmailAddress"
            e.Column.Visibility = Visibility.Visible
            Exit Select
        Case Else
            e.Column.Visibility = Visibility.Collapsed
            Exit Select

    End Select
End Sub

Private Sub CheckBox_Checked(ByVal sender As Object, ByVal e As RoutedEventArgs)
    'Make each column in the collection visible
    For Each col As DataGridColumn In DG1.Columns
        col.Visibility = Visibility.Visible
    Next
End Sub

Private Sub CheckBox_Unchecked(ByVal sender As Object, ByVal e As RoutedEventArgs)
    'Get the columns collection
    Dim columns As ObservableCollection(Of DataGridColumn) = DG1.Columns

    'set the visibility for each column so only 4 columns are visible
    For Each col As DataGridColumn In columns
        Select Case col.Header.ToString()
            Case "LastName"
                col.Visibility = Visibility.Visible
                Exit Select
            Case "FirstName"
                col.Visibility = Visibility.Visible
                Exit Select
            Case "CompanyName"
                col.Visibility = Visibility.Visible
                Exit Select
            Case "EmailAddress"
                col.Visibility = Visibility.Visible
                Exit Select
            Case Else
                col.Visibility = Visibility.Collapsed
                Exit Select
        End Select

    Next
End Sub

注解

使用 Columns 集合添加列、删除列或更新列的属性。

备注

集合中列的顺序不确定它们将显示在其中 DataGrid的顺序。 该 DisplayIndex 属性确定列顺序。

集合中的每个 Columns 列定义一 DataGrid个列。 下表列出了提供的 DataGrid 四个列类型。

列类型 数据显示
DataGridHyperlinkColumn 用于显示 URI 数据。
DataGridComboBoxColumn 用于显示枚举数据。
DataGridTextColumn 用于显示文本。
DataGridCheckBoxColumn 用于显示布尔数据。

此外,可以使用自己的自定义列来定义自己的自定义列 DataGridTemplateColumn。 集合中的列必须派生自 DataGridColumn. 请注意 DataGridBoundColumn,它增加了对绑定的支持,派生自 DataGridColumn 和为多个定义的列类型的基础。

All columns in the collection use the ItemsSource property defined by the DataGrid.

无论集合是否包含生成的列,都可以在运行时修改 Columns 集合。

适用于