ListView.HideSelection 属性

定义

获取或设置一个值,该值指示当控件没有焦点时,该控件中选定的项是否保持突出显示。

C#
public bool HideSelection { get; set; }

属性值

在控件没有焦点时,如果选定项不突出显示,则为 true;如果选定项在控件没有焦点时仍突出显示,则为 false

下表显示了不同 .NET 版本的此属性的默认值。

.NET 版本 默认值
.NET Core 3.0 及更高版本false
NET Framework 4.8 false,除非你已 选择退出新的辅助功能
.NET Framework 1.0 - 4.7true

示例

下面的代码示例演示 ListView 允许选择多个项的 。 该示例演示如何设置 HideSelectionHeaderStyle 属性。 它还演示了 ColumnHeader.TextColumnHeader.TextAlignColumnHeader.Width 属性。 若要运行此示例,请将以下代码粘贴到包含 ListView 名为 ListView1 的对象和名为 的 TextBoxTextBox1窗体中。 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);
  }

注解

当此属性设置为 false时,当控件失去焦点时ListViewListView控件中的选定项仍以与操作系统指定的当前选择颜色不同的颜色突出显示。 可以使用此属性在用户单击窗体上的其他控件或移动到其他窗口时使用户选择的项目可见。

备注

如果关注应用程序的可访问性,建议将 属性设置为 HideSelectionfalse

适用于

产品 版本
.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