ListViewItem.Group Eigenschap

Definitie

Hiermee wordt de groep opgehaald of ingesteld waaraan het item is toegewezen.

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

Waarde van eigenschap

Het ListViewGroup item waaraan het item is toegewezen.

Voorbeelden

In het volgende codevoorbeeld ziet u hoe de Group eigenschap kan worden gebruikt in een toepassing waarmee items worden ingedeeld ListView op subitemwaarde in de detailweergave. Deze vorm van groepering is vergelijkbaar met de groepering die wordt gebruikt in Windows Explorer. In het voorbeeld worden de groepen dynamisch gemaakt. Voor elke subitemkolom wordt één groep gemaakt voor elke unieke subitemwaarde. Voor de kolom met bovenliggende items wordt één groep gemaakt voor elke unieke eerste letter. De groepen die voor elke kolom worden gemaakt, worden samen met de subitemtekst of de eerste letter opgeslagen in een hash-tabel. Wanneer op een kolomkop wordt geklikt, wordt de hashtabel die overeenkomt met die kolom opgehaald. Vervolgens worden de tekstwaarden van de subitem voor die kolom gebruikt als hashtabelsleutels om de juiste groep voor elk item op te halen. Het item wordt vervolgens aan de groep toegewezen met behulp van de Group eigenschap.

Dit codevoorbeeld maakt deel uit van een groter voorbeeld voor de ListView.Groups eigenschap.

   // Sets myListView to the groups created for the specified column.
private:
   void SetGroups(int column)
   {
      // Remove the current groups.
      myListView->Groups->Clear();

      // Retrieve the hash table corresponding to the column.
      Hashtable^ groups = dynamic_cast<Hashtable^>(groupTables[column]);

      // Copy the groups for the column to an array.
      array<ListViewGroup^>^ groupsArray = gcnew array<ListViewGroup^>(groups->Count);
      groups->Values->CopyTo(groupsArray, 0);

      // Sort the groups and add them to myListView.
      Array::Sort(groupsArray, gcnew ListViewGroupSorter(myListView->Sorting));
      myListView->Groups->AddRange(groupsArray);

      // Iterate through the items in myListView, assigning each 
      // one to the appropriate group.
      IEnumerator^ myEnum = myListView->Items->GetEnumerator();
      while (myEnum->MoveNext())
      {
         ListViewItem^ item = safe_cast<ListViewItem^>(myEnum->Current);
         // Retrieve the subitem text corresponding to the column.
         String^ subItemText = item->SubItems[column]->Text;

         // For the Title column, use only the first letter.
         if (column == 0) 
         {
            subItemText = subItemText->Substring(0, 1);
         }

         // Assign the item to the matching group.
         item->Group = dynamic_cast<ListViewGroup^>(groups[subItemText]);
      }
   }
// Sets myListView to the groups created for the specified column.
private void SetGroups(int column)
{
    // Remove the current groups.
    myListView.Groups.Clear();

    // Retrieve the hash table corresponding to the column.
    Hashtable groups = (Hashtable)groupTables[column];

    // Copy the groups for the column to an array.
    ListViewGroup[] groupsArray = new ListViewGroup[groups.Count];
    groups.Values.CopyTo(groupsArray, 0);

    // Sort the groups and add them to myListView.
    Array.Sort(groupsArray, new ListViewGroupSorter(myListView.Sorting));
    myListView.Groups.AddRange(groupsArray);

    // Iterate through the items in myListView, assigning each 
    // one to the appropriate group.
    foreach (ListViewItem item in myListView.Items)
    {
        // Retrieve the subitem text corresponding to the column.
        string subItemText = item.SubItems[column].Text;

        // For the Title column, use only the first letter.
        if (column == 0) 
        {
            subItemText = subItemText.Substring(0, 1);
        }

        // Assign the item to the matching group.
        item.Group = (ListViewGroup)groups[subItemText];
    }
}
' Sets myListView to the groups created for the specified column.
Private Sub SetGroups(column As Integer)
    ' Remove the current groups.
    myListView.Groups.Clear()
    
    ' Retrieve the hash table corresponding to the column.
    Dim groups As Hashtable = CType(groupTables(column), Hashtable)
    
    ' Copy the groups for the column to an array.
    Dim groupsArray(groups.Count - 1) As ListViewGroup
    groups.Values.CopyTo(groupsArray, 0)
    
    ' Sort the groups and add them to myListView.
    Array.Sort(groupsArray, New ListViewGroupSorter(myListView.Sorting))
    myListView.Groups.AddRange(groupsArray)
    
    ' Iterate through the items in myListView, assigning each 
    ' one to the appropriate group.
    Dim item As ListViewItem
    For Each item In myListView.Items
        ' Retrieve the subitem text corresponding to the column.
        Dim subItemText As String = item.SubItems(column).Text
        
        ' For the Title column, use only the first letter.
        If column = 0 Then
            subItemText = subItemText.Substring(0, 1)
        End If 

        ' Assign the item to the matching group.
        item.Group = CType(groups(subItemText), ListViewGroup)
    Next item
End Sub

Opmerkingen

Gebruik deze eigenschap om de groep in te stellen waartoe een item behoort. U kunt de groep ook instellen in de ListViewItem constructor of u kunt deze eigenschap gebruiken om het groepslidmaatschap tijdens runtime te wijzigen. Als u deze eigenschap instelt op null en er groepen zijn in de verzameling ListView.Groups, wordt het item weergegeven in de standaardgroep, met het koptekstlabel DefaultGroupSystem. Windows. Formulieren". De standaardgroep is niet opgenomen in de ListView.Groups verzameling en kan niet worden gewijzigd. Het is vooral handig bij het opsporen van fouten om ervoor te zorgen dat alle items correct zijn toegevoegd aan groepen.

Note

ListView groepen zijn alleen beschikbaar op Windows XP en de Windows Server 2003-familie (Windows XP Home Edition, Windows XP Professional, Windows Server 2003). Zie het ListViewGroup overzichtsonderwerp voor meer informatie.

Van toepassing op

Zie ook