ListView.HeaderStyle 属性

定义

获取或设置列标题样式。

C#
public System.Windows.Forms.ColumnHeaderStyle HeaderStyle { get; set; }

属性值

ColumnHeaderStyle 值之一。 默认值为 Clickable

例外

指定的值不是 ColumnHeaderStyle 值之一。

示例

下面的代码示例演示 ListView 了允许选择多个项的 。 该示例演示如何设置 HideSelectionHeaderStyle 属性。 它还演示了 ColumnHeader.TextColumnHeader.TextAlignColumnHeader.Width 属性。 若要运行此示例,请将以下代码粘贴到包含 ListView 名为 ListView1 的对象和 TextBox 名为 的 TextBox1窗体中。 InitializeListView从窗体的构造函数或Load事件处理程序调用 方法。

C#
  // This method adds two columns to the ListView, setting the Text 
  // and TextAlign, and Width properties of each ColumnHeader.  The 
  // HeaderStyle property is set to NonClickable since the ColumnClick 
  // event is not handled.  Finally the method adds ListViewItems and 
  // SubItems to each column.
  private void InitializeListView()
  {
      this.ListView1 = new System.Windows.Forms.ListView();
      this.ListView1.BackColor = System.Drawing.SystemColors.Control;
      this.ListView1.Dock = System.Windows.Forms.DockStyle.Top;
      this.ListView1.Location = new System.Drawing.Point(0, 0);
      this.ListView1.Name = "ListView1";
      this.ListView1.Size = new System.Drawing.Size(292, 130);
      this.ListView1.TabIndex = 0;
      this.ListView1.View = System.Windows.Forms.View.Details;
      this.ListView1.MultiSelect = true;
      this.ListView1.HideSelection = false;
      this.ListView1.HeaderStyle = ColumnHeaderStyle.Nonclickable;
      
      ColumnHeader columnHeader1 = new ColumnHeader();
      columnHeader1.Text = "Breakfast Item";
      columnHeader1.TextAlign = HorizontalAlignment.Left;
      columnHeader1.Width = 146;

      ColumnHeader columnHeader2 = new ColumnHeader();
      columnHeader2.Text = "Price Each";
      columnHeader2.TextAlign = HorizontalAlignment.Center;
      columnHeader2.Width = 142;

      this.ListView1.Columns.Add(columnHeader1);
      this.ListView1.Columns.Add(columnHeader2);

      string[] foodList = new string[]{"Juice", "Coffee", 
          "Cereal & Milk", "Fruit Plate", "Toast & Jelly", 
          "Bagel & Cream Cheese"};
      string[] foodPrice = new string[]{"1.09", "1.09", "2.19", 
          "2.49", "1.49", "1.49"};
      
      for(int count=0; count < foodList.Length; count++)
      {
          ListViewItem listItem = new ListViewItem(foodList[count]);
          listItem.SubItems.Add(foodPrice[count]);
          ListView1.Items.Add(listItem);
      }
      this.Controls.Add(ListView1);
  }

注解

属性HeaderStyle允许你指定当 控件的 属性ListView设置为 DetailsListView 控件具有 ColumnHeader 中指定的ListView.ColumnHeaderCollection对象时View显示的列标题的类型。 ColumnHeader 对象定义控件中显示的 ListView 列。 每列用于显示 中每个项的 ListView子项信息。

属性 HeaderStyle 允许指定列标题是否可见,或者,如果列标题可见,它们是否将用作可单击按钮。 如果 属性 HeaderStyle 设置为 ColumnHeaderStyle.None,则不显示列标题,尽管控件的 ListView 项和子项仍按列排列。 如果 属性 HeaderStyle 设置为 ColumnHeaderStyle.Clickable,则列标题的作用类似于按钮,用户可以单击按钮来执行操作,例如使用单击列中的项 ListView 作为键对控件中的项进行排序。 可以在 事件的处理程序 ColumnClick 中实现此行为。 如果 属性 HeaderStyle 设置为 ColumnHeaderStyle.Nonclickable,则将显示列标题,但无法单击。

适用于

产品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10

另请参阅