ListView.View 属性

定义

获取或设置项在控件中的显示方式。Gets or sets how items are displayed in the control.

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 值之一。One of the View values. 默认值为 LargeIconThe default is LargeIcon.

异常

指定的值不是 View 值之一。The value specified is not one of the View values.

示例

下面的代码示例创建一个 @no__t 0 的控件,其中指定了三个 @no__t 对象和三个为每个项指定的 @no__t 2 个对象。The following code example creates a ListView control with three ListViewItem objects specified and three ListViewItem.ListViewSubItem objects specified for each item. 该示例还创建 @no__t 0 对象,以便在详细信息视图中显示子项。The example also creates ColumnHeader objects to display the subitems in details view. 在代码示例中,还会创建两个 @no__t 0 对象,以便为 @no__t 1 对象提供图像。Two ImageList objects are also created in the code example to provide images for the ListViewItem objects. 这些 ImageList 对象将添加到 @no__t 和 @no__t 2 属性。These ImageList objects are added to the LargeImageList and SmallImageList properties. 该示例使用以下属性创建 @no__t 0 控件。The example uses the following properties in creating the ListView control.

此示例要求您已将代码添加到 @no__t 0,并从构造函数或窗体上的其他方法调用在此示例中创建的方法。This example requires that you have added the code to a Form and call the method created in the example from the constructor or another method on the form. 该示例还要求在驱动器 C 的根目录中指定名为 MySmallImage1MySmallImage2、@no__t @no__t)的映像。The example also requires that images named MySmallImage1, MySmallImage2, MyLargeImage1, and MyLargeImage2 are located in the root directory of drive 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

注解

@No__t-0 属性允许您指定 @no__t 控件用于显示项的显示类型。The View property allows you to specify the type of display the ListView control uses to display items. 可以设置 View 属性以显示具有大图标或小图标的每个项,或者在垂直列表中显示项。You can set the View property to display each item with large or small icons or display items in a vertical list. 最丰富的选项是详细信息视图,它允许您不仅查看项,还可以查看为每个项指定的任何子项。The richest option is the details view, which allows you to view not only the items but any subitems specified for each item. 每一项都显示在网格中,其中每一项都垂直列出,每个项都显示在列中,具有列标题。Each item is displayed in a grid, with each item listed vertically and the subitems for each item displayed in a column, with column headers. 详细信息视图是向用户显示数据库信息的一种绝佳方式。The details view is a perfect way to display database information to a user. 使用 Windows XP 和 Windows Server 2003,你还可以将项显示为磁贴,通过显示大图标以及所选子项信息来平衡图形和文本信息。With Windows XP and Windows Server 2003, you can also display items as tiles that balance graphical and textual information by showing a large icon along with subitem information of your choosing. 若要启用磁贴视图,应用程序必须调用 Application.EnableVisualStyles 方法。To enable tile view, your application must call the Application.EnableVisualStyles method. 小型图像视图显示每个项,图标的右侧显示一个图标和文本信息。The small-image view displays each item with an icon and textual information to the right of the icon. 大图像视图显示每个项,图标的下方显示一个图标和文本信息。The large-image view displays each item with an icon and textual information below the icon. 图像列表的图标的大小由 SmallImageList 或 @no__t 属性的 @no__t 的 ImageList 属性指定。The size of the icons for the image list is specified by the ImageSize property of the ImageList for the SmallImageList or LargeImageList properties.

备注

如果使用的是多个图像列表(对于小图标视图和大图标视图,使用 @no__t 0 的控件),则应将图像的小型和大型版本放置在其各自的图像列表中的同一索引位置。If you are using multiple image lists, for small and large icon view, with a ListView control, you should place small and large versions of the image at the same index location in their respective image lists. 在视图之间切换时,使用一个列表中的图像的索引位置来查找另一个列表中的图像,而不考虑指定的键值。When switching between views, the index location of the image in one list is used to locate the image in the other list, regardless of the key value specified.

@No__t-0 控件中的大多数属性会影响不同视图的行为或显示方式。Most of the properties in the ListView control affect how the different views behave or are displayed. 某些影响项的视图的属性仅在 View 属性设置为特定值时才有用,而其他属性在所有视图中都很有用。Some properties that affect the views of the items are only useful when the View property is set to a specific value, while others are useful in all views. 例如,仅当 View 属性设置为 View.Details,而 @no__t 和 @no__t 属性在所有视图中都有用时,属性(例如 GridLinesFullRowSelect)才有用。For example, properties such as GridLines and FullRowSelect are only useful when the View property is set to View.Details, while the MultiSelect and CheckBoxes properties are useful in all views.

下表显示了一些 @no__t 的成员以及它们在中有效的视图。The following table shows some of the ListView members and the views they are valid in.

ListView 成员ListView member 视图View
Alignment 属性Alignment property SmallIconLargeIconSmallIcon or LargeIcon
AutoArrange 属性AutoArrange property SmallIconLargeIconSmallIcon or LargeIcon
AutoResizeColumn 方法AutoResizeColumn method Details
CheckBoxes 除 @no__t 以外的所有视图All views except Tile
Columns 属性Columns property DetailsTileDetails or Tile
DrawSubItem 事件DrawSubItem event Details
FindItemWithText 方法FindItemWithText method DetailsListTileDetails, List, or Tile
FindNearestItem 方法FindNearestItem method SmallIconLargeIconSmallIcon or LargeIcon
GetItemAt 方法GetItemAt method DetailsTileDetails or Tile
Groups 属性Groups property 除 @no__t 以外的所有视图All views except List
HeaderStyle 属性HeaderStyle property Details
InsertionMark 属性InsertionMark property LargeIconSmallIconTileLargeIcon, SmallIcon, or Tile

您可以使用 View 属性在您的应用程序中提供不同的数据视图,或锁定特定视图以利用该视图的优点。You can use the View property to provide different views of data in your application, or to lock a specific view to utilize that view's benefits. 例如,View 属性通常设置为 View.Details,因为详细信息视图提供了许多在其他视图中不可用的查看选项。For example, the View property is often set to View.Details because the details view provides a number of viewing options not available in the other views.

备注

如果 @no__t 0 的控件未指定任何列标题,并且您将 @no__t 属性设置为 View.Details,则 ListView 控件将不会显示任何项。If your ListView control does not have any column headers specified and you set the View property to View.Details, the ListView control will not display any items. 如果 @no__t 0 的控件未指定任何列标题,并且您将 @no__t 属性设置为 View.Tile,则 @no__t 的控件不会显示任何子项。If your ListView control does not have any column headers specified and you set the View property to View.Tile, the ListView control will not display any subitems.

磁贴视图显示每个项目,左侧有一个大图标,右侧有文本信息。The tile view displays each item with a large icon on the left and textual information on the right. 文本信息由项标签后跟子项组成。The textual information consists of the item label followed by subitems. 默认情况下,仅显示第一个子项,这对应于项标签。By default, only the first subitem is displayed, which corresponds to the item label. 若要显示其他子项,必须将 @no__t 0 对象添加到 @no__t 集合。To display additional subitems, you must add ColumnHeader objects to the Columns collection. 磁贴中的每个子项对应于一个列标题。Each subitem in the tile corresponds to a column header. 若要控制显示哪些子项以及显示它们的顺序,您必须为每个项设置每个项的 ListViewItem.ListViewSubItem.Name 属性和每个标头的 @no__t 属性。To control which subitems are displayed and the order in which they are displayed, you must set the ListViewItem.ListViewSubItem.Name property for each item and the ColumnHeader.Name property for each header. 然后,你可以添加、删除和重新排列 Columns 集合中的标头以获得所需的结果。You can then add, remove, and rearrange headers in the Columns collection to achieve the desired result.

若要在图块视图中控制磁贴的大小,请设置 TileSize 属性。To control the size of the tiles in the tile view, set the TileSize property. 这有助于防止子项文本对于单个行过长过长。This is useful to prevent line-wrapping when subitem text is too long for a single line.

有关图块视图的示例,请参阅 TileSize 属性。For an example of the tile view, see the TileSize property.

备注

尽管列只在详细信息视图中显示,但没有列标题的子项将不会显示在 "详细信息视图" 或 "磁贴" 视图中。Although columns are only displayed in the details view, subitems without column headers will not display in either the details view or the tile view.

当应用程序调用 Application.EnableVisualStyles 方法时,磁贴视图仅在 Windows XP 和 Windows Server 2003 上可用。The tile view is available only on Windows XP and Windows Server 2003 when your application calls the Application.EnableVisualStyles method. 在早期的操作系统上,任何与磁贴视图相关的代码都不起作用,且 ListView 控件将显示在大图标视图中。On earlier operating systems, any code related to the tile view has no effect, and the ListView control displays in the large icon view. 因此,依赖于磁贴视图的任何代码可能无法正常工作。As a result, any code that depends on the tile view might not work correctly.

你可能希望包含确定磁贴视图是否可用的代码,并在其不可用时提供其他功能。You might want to include code that determines whether tile view is available, and provide alternate functionality when it is unavailable. 例如,当你使用所有者描述自定义磁贴视图中 @no__t 0 项的外观时,你可能希望在不支持磁贴视图的操作系统上运行时,使用适用于大图标视图的绘制代码。For example, when you use owner drawing to customize the appearance of ListView items in the tile view, you might want to use drawing code appropriate to the large icon view when running on operating systems that do not support the tile view.

磁贴视图功能由提供操作系统主题功能的相同库提供。The tile view feature is provided by the same library that provides the operating system themes feature. 若要检查此库的可用性,请调用 @no__t 0 方法重载,并传入 OSFeature.Themes 值。To check for the availability of this library, call the FeatureSupport.IsPresent(Object) method overload and pass in the OSFeature.Themes value.

适用于

另请参阅