ListView.View 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
設定項目在控制中如何顯示。
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 時使用了以下屬性。
這個範例需要你已經將程式碼加入 a Form ,並從建構子或其他表單中的方法呼叫範例中建立的方法。 範例也要求名為 MySmallImage1、 MySmallImage2、 MyLargeImage1和 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 方法。 小影像檢視會顯示每個項目,圖示右側有圖示和文字資訊。 大型影像檢視會顯示每個項目,圖示下方有圖示和文字資訊。 圖片清單圖示的大小由 ImageSize for the SmallImageList or LargeImageList 屬性的ImageList屬性所決定。
備註
如果你使用多個圖片清單,分別是小圖示和大圖示,並且有 ListView 控制項,你應該將圖片的大小版本放在各自圖片列表中相同的索引位置。 在切換視圖時,會利用一個列表中圖片的索引位置來定位該圖片,無論指定為何鍵值。
控制項中的 ListView 大多數屬性會影響不同視圖的行為或顯示方式。 有些影響項目視圖的屬性只有在屬性 View 設定為特定值時才有用,而其他屬性則適用於所有視圖。 例如,像 和 這樣的性質GridLines只有在屬性View設為 View.Details時才有用,而 和 MultiSelectCheckBoxes 性質則在所有視圖中都有用。FullRowSelect
下表顯示部分 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.NameColumnHeader.Name屬性。 接著你可以在集合中 Columns 新增、移除和重新排列標頭,以達成理想的效果。
要控制圖塊檢視中圖塊的大小,請設定屬性 TileSize 。 這有助於防止子項目文字過長無法單行時的換行現象。
關於瓦片視圖的範例,請參見該 TileSize 屬性。
備註
雖然欄位僅在細節檢視中顯示,但沒有欄位標頭的子項目不會在詳細檢視或磁貼檢視中顯示。
磁貼檢視僅在 Windows XP 和 Windows Server 2003 上,當應用程式呼叫該 Application.EnableVisualStyles 方法時才可用。 在早期作業系統中,任何與圖塊檢視相關的程式碼都沒有影響, ListView 控制項會顯示在大型圖示檢視中。 因此,任何依賴磁磚檢視的程式碼可能無法正常運作。
你可能想包含判斷磁貼檢視是否可用的程式碼,並在無法使用時提供替代功能。 例如,當你使用擁有者繪圖來自訂圖塊檢視中物品的外觀 ListView 時,在不支援圖塊檢視的作業系統上運行時,你可能希望使用適合大型圖示檢視的繪圖程式碼。
圖塊檢視功能由提供作業系統主題功能的同一函式庫提供。 要檢查此函式庫的可用性,呼叫 FeatureSupport.IsPresent(Object) 方法過載並傳遞 OSFeature.Themes 該值。