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

範例

下列程式碼範例會建立控制項,其中指定三 ListViewItem 個物件,以及針對每個專案指定的三 ListViewItem.ListViewSubItemListView 物件。 此範例也會建立 ColumnHeader 物件,以顯示詳細資料檢視中的子專案。 程式碼範例中也會建立兩 ImageList 個 物件,以提供 物件的影像 ListViewItem 。 這些 ImageList 物件會新增至 LargeImageListSmallImageList 屬性。 此範例會在建立 ListView 控制項時使用下列屬性。

此範例會要求您將程式碼新增至 Form ,並從表單上的建構函式或其他方法呼叫在範例中建立的方法。 此範例也需要名為 MySmallImage1MySmallImage2MyLargeImage1 和 的 MyLargeImage2 映射位於磁片磁碟機 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 時,例如 GridLinesFullRowSelect 等屬性才有用,而 MultiSelectCheckBoxes 屬性在所有檢視中都 View 很有用。

下表顯示其中一些 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如果您的控制項未指定任何資料行標頭,而且您已將 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 值。

適用於

另請參閱