Udostępnij za pośrednictwem


ListViewItem.Group Właściwość

Definicja

Pobiera lub ustawia grupę, do której jest przypisany element.

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

Wartość właściwości

ListViewGroup

Element ListViewGroup , do którego jest przypisany element.

Przykłady

Poniższy przykład kodu pokazuje, jak Group właściwość może być używana w aplikacji, która organizuje ListView elementy według wartości subitem w widoku szczegółów. Ta forma grupowania jest podobna do grupowania używanego w Eksploratorze Windows. W tym przykładzie grupy są tworzone dynamicznie. Dla każdej kolumny subitem jedna grupa jest tworzona dla każdej unikatowej wartości subitem. Dla kolumny elementu nadrzędnego jedna grupa jest tworzona dla każdej unikatowej litery początkowej. Grupy utworzone dla każdej kolumny są przechowywane w tabeli skrótów wraz z tekstem podrzędnym lub literą początkową. Po kliknięciu nagłówka kolumny zostanie pobrana tabela skrótów odpowiadająca tej kolumnie. Następnie wartości tekstowe subitem dla tej kolumny są używane jako klucze tabeli skrótów, aby pobrać poprawną grupę dla każdego elementu. Element jest następnie przypisywany do grupy przy użyciu Group właściwości .

Ten przykład kodu jest częścią większego przykładu podanego ListView.Groups dla właściwości.

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

Uwagi

Użyj tej właściwości, aby ustawić grupę, do której należy element. Możesz również ustawić grupę w konstruktorze ListViewItem lub użyć tej właściwości, aby zmodyfikować członkostwo w grupie w czasie wykonywania. Jeśli ustawisz tę właściwość na null wartość i istnieją grupy w ListView.Groups kolekcji, element pojawi się w grupie domyślnej, która ma etykietę nagłówka "DefaultGroupSystem.Windows.Forms". Grupa domyślna nie jest zawarta w ListView.Groups kolekcji i nie można jej zmienić. Jest to przede wszystkim przydatne podczas debugowania, aby upewnić się, że wszystkie elementy zostały prawidłowo dodane do grup.

Uwaga

ListView grupy są dostępne tylko w systemie Windows XP i rodzinie windows Server 2003 (Windows XP Home Edition, Windows XP Professional, Windows Server 2003). Aby uzyskać więcej informacji, zobacz ListViewGroup temat omówienie.

Dotyczy

Zobacz też