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 值之一。

示例

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

此示例要求已将代码添加到 , Form 并从构造函数或窗体上的另一个方法调用在示例中创建的方法。 该示例还要求名为 MySmallImage1MySmallImage2MyLargeImage1MyLargeImage2 的映像位于驱动器 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 方法。 小图像视图在图标右侧显示带有图标和文本信息的每个项。 大图像视图在图标下方显示带有图标和文本信息的每个项。 图像列表的图标大小由 ImageSizeLargeImageList 属性的 ImageListSmallImageList 属性指定。

注意

如果使用多个图像列表,对于小型和大型图标视图,应使用 ListView 控件将小型和大型图像版本放置在其各自的图像列表中的同一索引位置。 在视图之间切换时,一个列表中的图像的索引位置用于查找另一个列表中的图像,而不考虑指定的键值。

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

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

ListView 成员 视图
Alignment 属性 SmallIconLargeIcon
AutoArrange 属性 SmallIconLargeIcon
AutoResizeColumn 方法 Details
CheckBoxes Tile 外的所有视图
Columns 属性 DetailsTile
DrawSubItem 事件 Details
FindItemWithText 方法 DetailsListTile
FindNearestItem 方法 SmallIconLargeIcon
GetItemAt 方法 DetailsTile
Groups 属性 List 外的所有视图
HeaderStyle 属性 Details
InsertionMark 属性 LargeIconSmallIconTile

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

注意

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

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

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

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

注意

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

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

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

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

适用于

另请参阅