Sdílet prostřednictvím


ListViewItem.Group Vlastnost

Definice

Získá nebo nastaví skupinu, ke které je položka přiřazena.

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

Hodnota vlastnosti

ListViewGroup

Položka ListViewGroup , ke které je položka přiřazena.

Příklady

Následující příklad kódu ukazuje, jak Group lze vlastnost použít v aplikaci, která uspořádá ListView položky podle hodnoty subitem v zobrazení podrobností. Tato forma seskupení je podobná seskupení použitému v Průzkumníku Windows. V tomto příkladu se skupiny vytvářejí dynamicky. Pro každý sloupec podsítě se vytvoří jedna skupina pro každou jedinečnou hodnotu podsítě. Pro sloupec nadřazené položky se vytvoří jedna skupina pro každé jedinečné počáteční písmeno. Skupiny vytvořené pro každý sloupec jsou uloženy v tabulce hash spolu s textem subitem nebo počátečním písmenem. Po kliknutí na záhlaví sloupce se načte tabulka hash odpovídající danému sloupci. Dále se jako klíče tabulky hash používají textové hodnoty subitem pro daný sloupec k načtení správné skupiny pro každou položku. Položka se pak přiřadí skupině pomocí Group vlastnosti.

Tento příklad kódu je součástí většího příkladu ListView.Groups zadaného pro vlastnost.

   // 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

Poznámky

Tuto vlastnost použijte k nastavení skupiny, do které položka patří. Můžete také nastavit skupinu v konstruktoru ListViewItem nebo můžete tuto vlastnost použít k úpravě členství ve skupině za běhu. Pokud tuto vlastnost nastavíte na null a v ListView.Groups kolekci jsou skupiny, položka se zobrazí ve výchozí skupině, která má popisek záhlaví DefaultGroupSystem.Windows.Forms. Výchozí skupina není obsažena v kolekci ListView.Groups a nelze ji změnit. Primárně je užitečné v ladění zajistit, aby všechny položky byly správně přidány do skupin.

Poznámka

ListView skupiny jsou k dispozici pouze v systémech Windows XP a řady Windows Server 2003 (Windows XP Home Edition, Windows XP Professional, Windows Server 2003). Další informace najdete v tématu s přehledem ListViewGroup .

Platí pro

Viz také