语言

ListView.View 属性

定义

获取或设置控件中的项的显示方式。

public:
 property System::Windows::Forms::View View { System::Windows::Forms::View get(); void set(System::Windows::Forms::View value); };
public System.Windows.Forms.View View { get; set; }
member this.View : System.Windows.Forms.View with get, set
Public Property View As View

属性值

其中一个 View 值。 默认值为 LargeIcon。

例外

指定的值不是值之 View 一。

示例

下面的代码示例创建一个控件,该控件指定了三ListView个ListViewItem对象,以及为每个项指定的三ListViewItem.ListViewSubItem个对象。 该示例还创建 ColumnHeader 对象以显示详细信息视图中的子项。 在代码示例中还创建了两 ImageList 个对象,用于为 ListViewItem 对象提供图像。 这些 ImageList 对象将添加到 LargeImageList 属性中 SmallImageList 。 该示例在创建 ListView 控件时使用以下属性。

此示例要求你已将代码添加到一个 Form ,并从窗体上的构造函数或其他方法调用在示例中创建的方法。 该示例还要求名为 MySmallImage1C MySmallImage2MyLargeImage1MyLargeImage2 的映像位于驱动器 C 的根目录中。

private:
   void CreateMyListView()
   {
      // Create a new ListView control.
      ListView^ listView1 = gcnew ListView;
      listView1->Bounds = Rectangle(Point(10,10),System::Drawing::Size( 300, 200 ));

      // Set the view to show details.
      listView1->View = View::Details;

      // Allow the user to edit item text.
      listView1->LabelEdit = true;

      // Allow the user to rearrange columns.
      listView1->AllowColumnReorder = true;

      // Display check boxes.
      listView1->CheckBoxes = true;

      // Select the item and subitems when selection is made.
      listView1->FullRowSelect = true;

      // Display grid lines.
      listView1->GridLines = true;

      // Sort the items in the list in ascending order.
      listView1->Sorting = SortOrder::Ascending;

      // Create three items and three sets of subitems for each item.
      ListViewItem^ item1 = gcnew ListViewItem( "item1",0 );

      // Place a check mark next to the item.
      item1->Checked = true;
      item1->SubItems->Add( "1" );
      item1->SubItems->Add( "2" );
      item1->SubItems->Add( "3" );
      ListViewItem^ item2 = gcnew ListViewItem( "item2",1 );
      item2->SubItems->Add( "4" );
      item2->SubItems->Add( "5" );
      item2->SubItems->Add( "6" );
      ListViewItem^ item3 = gcnew ListViewItem( "item3",0 );

      // Place a check mark next to the item.
      item3->Checked = true;
      item3->SubItems->Add( "7" );
      item3->SubItems->Add( "8" );
      item3->SubItems->Add( "9" );

      // Create columns for the items and subitems.
      // Width of -2 indicates auto-size.
      listView1->Columns->Add( "Item Column", -2, HorizontalAlignment::Left );
      listView1->Columns->Add( "Column 2", -2, HorizontalAlignment::Left );
      listView1->Columns->Add( "Column 3", -2, HorizontalAlignment::Left );
      listView1->Columns->Add( "Column 4", -2, HorizontalAlignment::Center );

      //Add the items to the ListView.
      array<ListViewItem^>^temp1 = {item1,item2,item3};
      listView1->Items->AddRange( temp1 );

      // Create two ImageList objects.
      ImageList^ imageListSmall = gcnew ImageList;
      ImageList^ imageListLarge = gcnew ImageList;

      // Initialize the ImageList objects with bitmaps.
      imageListSmall->Images->Add( Bitmap::FromFile( "C:\\MySmallImage1.bmp" ) );
      imageListSmall->Images->Add( Bitmap::FromFile( "C:\\MySmallImage2.bmp" ) );
      imageListLarge->Images->Add( Bitmap::FromFile( "C:\\MyLargeImage1.bmp" ) );
      imageListLarge->Images->Add( Bitmap::FromFile( "C:\\MyLargeImage2.bmp" ) );

      //Assign the ImageList objects to the ListView.
      listView1->LargeImageList = imageListLarge;
      listView1->SmallImageList = imageListSmall;
      
      // Add the ListView to the control collection.
      this->Controls->Add( listView1 );
   }
private void CreateMyListView()
{
    // Create a new ListView control.
    ListView listView1 = new ListView();
    listView1.Bounds = new Rectangle(new Point(10,10), new Size(300,200));

    // Set the view to show details.
    listView1.View = View.Details;
    // Allow the user to edit item text.
    listView1.LabelEdit = true;
    // Allow the user to rearrange columns.
    listView1.AllowColumnReorder = true;
    // Display check boxes.
    listView1.CheckBoxes = true;
    // Select the item and subitems when selection is made.
    listView1.FullRowSelect = true;
    // Display grid lines.
    listView1.GridLines = true;
    // Sort the items in the list in ascending order.
    listView1.Sorting = SortOrder.Ascending;
                
    // Create three items and three sets of subitems for each item.
    ListViewItem item1 = new ListViewItem("item1",0);
    // Place a check mark next to the item.
    item1.Checked = true;
    item1.SubItems.Add("1");
    item1.SubItems.Add("2");
    item1.SubItems.Add("3");
    ListViewItem item2 = new ListViewItem("item2",1);
    item2.SubItems.Add("4");
    item2.SubItems.Add("5");
    item2.SubItems.Add("6");
    ListViewItem item3 = new ListViewItem("item3",0);
    // Place a check mark next to the item.
    item3.Checked = true;
    item3.SubItems.Add("7");
    item3.SubItems.Add("8");
    item3.SubItems.Add("9");

    // Create columns for the items and subitems.
    // Width of -2 indicates auto-size.
    listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left);
    listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left);
    listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left);
    listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center);

    //Add the items to the ListView.
    listView1.Items.AddRange(new ListViewItem[]{item1,item2,item3});

    // Create two ImageList objects.
    ImageList imageListSmall = new ImageList();
    ImageList imageListLarge = new ImageList();

    // Initialize the ImageList objects with bitmaps.
    imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage1.bmp"));
    imageListSmall.Images.Add(Bitmap.FromFile("C:\\MySmallImage2.bmp"));
    imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage1.bmp"));
    imageListLarge.Images.Add(Bitmap.FromFile("C:\\MyLargeImage2.bmp"));

    //Assign the ImageList objects to the ListView.
    listView1.LargeImageList = imageListLarge;
    listView1.SmallImageList = imageListSmall;

    // Add the ListView to the control collection.
    this.Controls.Add(listView1);
}
Private Sub CreateMyListView()
    ' Create a new ListView control.
    Dim listView1 As New ListView()
    listView1.Bounds = New Rectangle(New Point(10, 10), New Size(300, 200))

    ' Set the view to show details.
    listView1.View = View.Details
    ' Allow the user to edit item text.
    listView1.LabelEdit = True
    ' Allow the user to rearrange columns.
    listView1.AllowColumnReorder = True
    ' Display check boxes.
    listView1.CheckBoxes = True
    ' Select the item and subitems when selection is made.
    listView1.FullRowSelect = True
    ' Display grid lines.
    listView1.GridLines = True
    ' Sort the items in the list in ascending order.
    listView1.Sorting = SortOrder.Ascending

    ' Create three items and three sets of subitems for each item.
    Dim item1 As New ListViewItem("item1", 0)
    ' Place a check mark next to the item.
    item1.Checked = True
    item1.SubItems.Add("1")
    item1.SubItems.Add("2")
    item1.SubItems.Add("3")
    Dim item2 As New ListViewItem("item2", 1)
    item2.SubItems.Add("4")
    item2.SubItems.Add("5")
    item2.SubItems.Add("6")
    Dim item3 As New ListViewItem("item3", 0)
    ' Place a check mark next to the item.
    item3.Checked = True
    item3.SubItems.Add("7")
    item3.SubItems.Add("8")
    item3.SubItems.Add("9")

    ' Create columns for the items and subitems.
    ' Width of -2 indicates auto-size.
    listView1.Columns.Add("Item Column", -2, HorizontalAlignment.Left)
    listView1.Columns.Add("Column 2", -2, HorizontalAlignment.Left)
    listView1.Columns.Add("Column 3", -2, HorizontalAlignment.Left)
    listView1.Columns.Add("Column 4", -2, HorizontalAlignment.Center)

    'Add the items to the ListView.
    listView1.Items.AddRange(New ListViewItem() {item1, item2, item3})

    ' Create two ImageList objects.
    Dim imageListSmall As New ImageList()
    Dim imageListLarge As New ImageList()

    ' Initialize the ImageList objects with bitmaps.
    imageListSmall.Images.Add(Bitmap.FromFile("C:\MySmallImage1.bmp"))
    imageListSmall.Images.Add(Bitmap.FromFile("C:\MySmallImage2.bmp"))
    imageListLarge.Images.Add(Bitmap.FromFile("C:\MyLargeImage1.bmp"))
    imageListLarge.Images.Add(Bitmap.FromFile("C:\MyLargeImage2.bmp"))

    'Assign the ImageList objects to the ListView.
    listView1.LargeImageList = imageListLarge
    listView1.SmallImageList = imageListSmall

    ' Add the ListView to the control collection.
    Me.Controls.Add(listView1)
End Sub

注解

使用该 View 属性可以指定控件用于显示项的显示 ListView 类型。 可以设置该 View 属性,以显示每个项目,其中包含大图标或小图标或垂直列表中的项目。 最丰富的选项是详细信息视图,它允许你不仅查看项,还可以查看为每个项指定的任何子项。 每个项都显示在网格中,每个项垂直列出,每个项都以列标题显示每个项的子项。 详细信息视图是向用户显示数据库信息的完美方法。 借助 Windows XP 和 Windows Server 2003,还可以通过显示大型图标以及你选择的子项信息,将项显示为平衡图形和文本信息的磁贴。 若要启用磁贴视图,应用程序必须调用 Application.EnableVisualStyles 该方法。 小图像视图显示每个项,并在图标右侧显示一个图标和文本信息。 大图像视图显示每个项,其图标和文本信息位于图标下方。 图像列表的图标大小由ImageSize该或ImageList属性的属性SmallImageListLargeImageList指定。

注释

如果使用多个图像列表(对于小型和大型图标视图),则 ListView 应该将图像的小型和大型版本放置在其各自的图像列表中的同一索引位置。 在视图之间切换时,无论指定的键值如何,一个列表中的图像的索引位置都用于查找另一个列表中的图像。

控件中的 ListView 大多数属性会影响不同视图的行为或显示方式。 某些影响项视图的属性仅在属性 View 设置为特定值时才有用,而其他属性在所有视图中都很有用。 例如,属性等GridLinesFullRowSelect属性仅在属性设置为View时有用View.Details,而MultiSelectCheckBoxes属性在所有视图中都很有用。

下表显示了一些 ListView 成员及其有效视图。

ListView 成员 查看
Alignment 属性 SmallIcon 或 LargeIcon
AutoArrange 属性 SmallIcon 或 LargeIcon
AutoResizeColumn 方法 Details
CheckBoxes 除 Tile
Columns 属性 Details 或 Tile
DrawSubItem 事件 Details
FindItemWithText 方法 Details、List 或 Tile
FindNearestItem 方法 SmallIcon 或 LargeIcon
GetItemAt 方法 Details 或 Tile
Groups 属性 除 List
HeaderStyle 属性 Details
InsertionMark 属性 LargeIcon、SmallIcon 或 Tile

可以使用该 View 属性在应用程序中提供不同的数据视图,或锁定特定视图以利用该视图的优势。 例如, View 属性通常设置为 View.Details 因为详细信息视图提供了许多在其他视图中不可用的查看选项。

注释

ListView如果控件未指定任何列标题,并且已将View属性设置为该控件View.Details,该ListView控件将不会显示任何项。 ListView如果控件未指定任何列标题,并且已将View属性设置为该控件View.Tile,该ListView控件将不会显示任何子项。

磁贴视图显示每个项,左侧有一个大图标,右侧有文本信息。 文本信息由项标签后跟子项组成。 默认情况下,仅显示第一个子项,对应于项标签。 若要显示其他子项,必须将对象添加到ColumnHeaderColumns集合中。 磁贴中的每个子项对应于列标题。 若要控制显示哪些子项及其显示顺序,必须为每个 ListViewItem.ListViewSubItem.Name 项设置属性以及 ColumnHeader.Name 每个标头的属性。 然后,可以在集合中添加 Columns 、删除和重新排列标头,以实现所需的结果。

若要控制磁贴视图中磁贴的大小,请设置 TileSize 属性。 当子项文本对于单行太长时,这可用于防止换行。

有关磁贴视图的示例,请参阅 TileSize 该属性。

注释

尽管列仅显示在详细信息视图中,但不包含列标题的子项将不会显示在详细信息视图或磁贴视图中。

当应用程序调用 Application.EnableVisualStyles 该方法时,磁贴视图仅在 Windows XP 和 Windows Server 2003 上可用。 在早期操作系统上,与磁贴视图相关的任何代码都不起作用,并且 ListView 控件显示在大型图标视图中。 因此,依赖于磁贴视图的任何代码可能无法正常工作。

你可能想要包含用于确定磁贴视图是否可用的代码,并在不可用时提供备用功能。 例如,使用所有者绘图自定义磁贴视图中项目的外观 ListView 时,你可能希望在不支持磁贴视图的操作系统上运行适合大型图标视图的绘图代码。

磁贴视图功能由提供操作系统主题功能的同一库提供。 若要检查此库的可用性,请调用 FeatureSupport.IsPresent(Object) 方法重载并传入 OSFeature.Themes 值。

适用于

另请参阅