ListViewGroup ListViewGroup ListViewGroup ListViewGroup Class

Definition

Stellt eine Gruppe von Elementen dar, die in einem ListView-Steuerelements angezeigt wird.Represents a group of items displayed within a ListView control.

public ref class ListViewGroup sealed : System::Runtime::Serialization::ISerializable
[System.ComponentModel.TypeConverter(typeof(System.Windows.Forms.ListViewGroupConverter))]
[System.Serializable]
public sealed class ListViewGroup : System.Runtime.Serialization.ISerializable
type ListViewGroup = class
    interface ISerializable
Public NotInheritable Class ListViewGroup
Implements ISerializable
Vererbung
ListViewGroupListViewGroupListViewGroupListViewGroup
Attribute
Implementiert

Beispiele

Im folgenden Codebeispiel wird veranschaulicht, wie die ListView Gruppierungs Funktion verwendet wird, um Elemente nach Unterelement-Wert in der Detailansicht zu organisieren.The following code example demonstrates how to use the ListView grouping feature to organize items by subitem value in the details view. Diese Form der Gruppierung ähnelt der im Windows-Explorer verwendeten Gruppierung.This form of grouping is similar to the grouping used in Windows Explorer. Im Beispiel werden die Gruppen dynamisch erstellt.In the example, the groups are created dynamically. Für jede Unterelement Spalte wird eine Gruppe für jeden eindeutigen Unterelement Wert erstellt.For each subitem column, one group is created for each unique subitem value. Für die übergeordnete Element Spalte wird eine Gruppe für jeden eindeutigen Anfangsbuchstaben erstellt.For the parent item column, one group is created for each unique initial letter. Wenn Sie auf den Header einer Spalte klicken, werden die Elemente in die für diese Spalte erstellten Gruppen sortiert.Clicking the header of a column sorts the items into the groups created for that column. Wenn Sie erneut auf denselben Spaltenheader klicken, wird die Reihenfolge der Gruppen umgekehrt.Clicking the same column header again reverses the order of the groups.

#using <mscorlib.dll>
#using <System.Drawing.dll>
#using <System.dll>
#using <System.Windows.Forms.dll>
using namespace System;
using namespace System::Collections; 
using namespace System::Windows::Forms;

public ref class ListViewGroupsExample : public Form
{
private:
   ListView^ myListView;
   bool isRunningXPOrLater;

   // Declare a Hashtable array in which to store the groups.
   array<Hashtable^>^ groupTables;

   // Declare a variable to store the current grouping column.
   int groupColumn;

public:
   ListViewGroupsExample()
   {
      groupColumn = 0;
      // Initialize myListView.
      myListView = gcnew ListView();
      myListView->Dock = DockStyle::Fill;
      myListView->View = View::Details;
      myListView->Sorting = SortOrder::Ascending;

      // Create and initialize column headers for myListView.
      ColumnHeader^ columnHeader0 = gcnew ColumnHeader();
      columnHeader0->Text = "Title";
      columnHeader0->Width = -1;
      ColumnHeader^ columnHeader1 = gcnew ColumnHeader();
      columnHeader1->Text = "Author";
      columnHeader1->Width = -1;
      ColumnHeader^ columnHeader2 = gcnew ColumnHeader();
      columnHeader2->Text = "Year";
      columnHeader2->Width = -1;

      // Add the column headers to myListView.

      array<ColumnHeader^>^ temp0 = {columnHeader0, columnHeader1, columnHeader2};
      myListView->Columns->AddRange(temp0);

      // Add a handler for the ColumnClick event.
      myListView->ColumnClick += 
         gcnew ColumnClickEventHandler(this, &ListViewGroupsExample::myListView_ColumnClick);

      // Create items and add them to myListView.

      array<String^>^ temp1 = {"Programming Windows", "Petzold, Charles", "1998"};
      ListViewItem^ item0 = gcnew ListViewItem( temp1 );

      array<String^>^ temp2 = {"Code: The Hidden Language of Computer Hardware and Software",
         "Petzold, Charles", "2000"};
      ListViewItem^ item1 = gcnew ListViewItem( temp2 );

      array<String^>^ temp3 = {"Programming Windows with C#", "Petzold, Charles", "2001"};
      ListViewItem^ item2 = gcnew ListViewItem( temp3 );

      array<String^>^ temp4 = {"Coding Techniques for Microsoft Visual Basic .NET",
         "Connell, John", "2001"};
      ListViewItem^ item3 = gcnew ListViewItem( temp4 );

      array<String^>^ temp5 = {"C# for Java Developers", "Jones, Allen & Freeman, Adam",
         "2002"};
      ListViewItem^ item4 = gcnew ListViewItem( temp5 );

      array<String^>^ temp6 = {"Microsoft .NET XML Web Services Step by Step",
         "Jones, Allen & Freeman, Adam", "2002"};
      ListViewItem^ item5 = gcnew ListViewItem( temp6 );

      array<ListViewItem^>^ temp7 = {item0, item1, item2, item3, item4, item5};
      myListView->Items->AddRange( temp7 );

      // Determine whether Windows XP or a later 
      // operating system is present.
      isRunningXPOrLater = false;

      if (System::Environment::OSVersion->Version->Major > 5 ||
         ( System::Environment::OSVersion->Version->Major == 5 &&
         System::Environment::OSVersion->Version->Minor >= 1) )
      {
         isRunningXPOrLater = true;
      }

      if (isRunningXPOrLater)
      {
         // Create the groupsTable array and populate it with one 
         // hash table for each column.
         groupTables = gcnew array<Hashtable^>(myListView->Columns->Count);
         for (int column = 0; column < myListView->Columns->Count; column++)
         {
            // Create a hash table containing all the groups 
            // needed for a single column.
            groupTables[column] = CreateGroupsTable(column);
         }

         // Start with the groups created for the Title column.
         SetGroups(0);
      }

      // Initialize the form.
      this->Controls->Add(myListView);
      this->Size = System::Drawing::Size(550, 330);
      this->Text = "ListView Groups Example";
   }

   // Groups the items using the groups created for the clicked 
   // column.
private:
   void myListView_ColumnClick(
      Object^ /*sender*/, ColumnClickEventArgs^ e)
   {
      // Set the sort order to ascending when changing
      // column groups; otherwise, reverse the sort order.
      if ( myListView->Sorting == SortOrder::Descending || 
         ( isRunningXPOrLater && (e->Column != groupColumn) ) )
      {
         myListView->Sorting = SortOrder::Ascending;
      }
      else 
      {
         myListView->Sorting = SortOrder::Descending;
      }
      groupColumn = e->Column;

      // Set the groups to those created for the clicked column.
      if (isRunningXPOrLater)
      {
         SetGroups(e->Column);
      }
   }

   // 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]);
      }
   }

   // Creates a Hashtable object with one entry for each unique
   // subitem value (or initial letter for the parent item)
   // in the specified column.
private:
   Hashtable^ CreateGroupsTable(int column)
   {
      // Create a Hashtable object.
      Hashtable^ groups = gcnew Hashtable();

      // Iterate through the items in myListView.
      IEnumerator^ myEnum1 = myListView->Items->GetEnumerator();
      while (myEnum1->MoveNext())
      {
         ListViewItem^ item = safe_cast<ListViewItem^>(myEnum1->Current);
         // Retrieve the text value for the column.
         String^ subItemText = item->SubItems[column]->Text;

         // Use the initial letter instead if it is the first column.
         if (column == 0) 
         {
            subItemText = subItemText->Substring(0, 1);
         }

         // If the groups table does not already contain a group
         // for the subItemText value, add a new group using the 
         // subItemText value for the group header and Hashtable key.
         if (!groups->Contains(subItemText))
         {
            groups->Add( subItemText, gcnew ListViewGroup(subItemText, 
               HorizontalAlignment::Left) );
         }
      }

      // Return the Hashtable object.
      return groups;
   }

   // Sorts ListViewGroup objects by header value.
   ref class ListViewGroupSorter : public IComparer
   {
   private:
      SortOrder order;

      // Stores the sort order.
   public:
      ListViewGroupSorter(SortOrder theOrder) 
      { 
         order = theOrder;
      }

      // Compares the groups by header value, using the saved sort
      // order to return the correct value.
      virtual int Compare(Object^ x, Object^ y)
      {
         int result = String::Compare(
            (dynamic_cast<ListViewGroup^>(x))->Header,
            (dynamic_cast<ListViewGroup^>(y))->Header
            );
         if (order == SortOrder::Ascending)
         {
            return result;
         }
         else 
         {
            return -result;
         }
      }
   };
};

[STAThread]
int main() 
{
   Application::EnableVisualStyles();
   Application::Run(gcnew ListViewGroupsExample());
}
using System;
using System.Collections; 
using System.Windows.Forms;

public class ListViewGroupsExample : Form
{
    private ListView myListView;

    // Determine whether Windows XP or a later
    // operating system is present.
    private bool isRunningXPOrLater = 
        OSFeature.Feature.IsPresent(OSFeature.Themes);

    // Declare a Hashtable array in which to store the groups.
    private Hashtable[] groupTables;

    // Declare a variable to store the current grouping column.
    int groupColumn = 0;

    public ListViewGroupsExample()
    {
        // Initialize myListView.
        myListView = new ListView();
        myListView.Dock = DockStyle.Fill;
        myListView.View = View.Details;
        myListView.Sorting = SortOrder.Ascending;

        // Create and initialize column headers for myListView.
        ColumnHeader columnHeader0 = new ColumnHeader();
        columnHeader0.Text = "Title";
        columnHeader0.Width = -1;
        ColumnHeader columnHeader1 = new ColumnHeader();
        columnHeader1.Text = "Author";
        columnHeader1.Width = -1;
        ColumnHeader columnHeader2 = new ColumnHeader();
        columnHeader2.Text = "Year";
        columnHeader2.Width = -1;

        // Add the column headers to myListView.
        myListView.Columns.AddRange(new ColumnHeader[] 
            {columnHeader0, columnHeader1, columnHeader2});

        // Add a handler for the ColumnClick event.
        myListView.ColumnClick += 
            new ColumnClickEventHandler(myListView_ColumnClick);

        // Create items and add them to myListView.
        ListViewItem item0 = new ListViewItem( new string[] 
            {"Programming Windows", 
            "Petzold, Charles", 
            "1998"} );
        ListViewItem item1 = new ListViewItem( new string[] 
            {"Code: The Hidden Language of Computer Hardware and Software", 
            "Petzold, Charles", 
            "2000"} );
        ListViewItem item2 = new ListViewItem( new string[] 
            {"Programming Windows with C#", 
            "Petzold, Charles", 
            "2001"} );
        ListViewItem item3 = new ListViewItem( new string[] 
            {"Coding Techniques for Microsoft Visual Basic .NET", 
            "Connell, John", 
            "2001"} );
        ListViewItem item4 = new ListViewItem( new string[] 
            {"C# for Java Developers", 
            "Jones, Allen & Freeman, Adam", 
            "2002"} );
        ListViewItem item5 = new ListViewItem( new string[] 
            {"Microsoft .NET XML Web Services Step by Step", 
            "Jones, Allen & Freeman, Adam", 
            "2002"} );
        myListView.Items.AddRange(
            new ListViewItem[] {item0, item1, item2, item3, item4, item5});

        if (isRunningXPOrLater)
        {
            // Create the groupsTable array and populate it with one 
            // hash table for each column.
            groupTables = new Hashtable[myListView.Columns.Count];
            for (int column = 0; column < myListView.Columns.Count; column++)
            {
                // Create a hash table containing all the groups 
                // needed for a single column.
                groupTables[column] = CreateGroupsTable(column);
            }

            // Start with the groups created for the Title column.
            SetGroups(0);
        }

        // Initialize the form.
        this.Controls.Add(myListView);
        this.Size = new System.Drawing.Size(550, 330);
        this.Text = "ListView Groups Example";
    }

    [STAThread]
    static void Main() 
    {
        Application.EnableVisualStyles();
        Application.Run(new ListViewGroupsExample());
    }

    // Groups the items using the groups created for the clicked 
    // column.
    private void myListView_ColumnClick(
        object sender, ColumnClickEventArgs e)
    {
        // Set the sort order to ascending when changing
        // column groups; otherwise, reverse the sort order.
        if ( myListView.Sorting == SortOrder.Descending || 
            ( isRunningXPOrLater && (e.Column != groupColumn) ) )
        {
            myListView.Sorting = SortOrder.Ascending;
        }
        else 
        {
            myListView.Sorting = SortOrder.Descending;
        }
        groupColumn = e.Column;

        // Set the groups to those created for the clicked column.
        if (isRunningXPOrLater)
        {
            SetGroups(e.Column);
        }
    }

    // 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];
        }
    }

    // Creates a Hashtable object with one entry for each unique
    // subitem value (or initial letter for the parent item)
    // in the specified column.
    private Hashtable CreateGroupsTable(int column)
    {
        // Create a Hashtable object.
        Hashtable groups = new Hashtable();

        // Iterate through the items in myListView.
        foreach (ListViewItem item in myListView.Items)
        {
            // Retrieve the text value for the column.
            string subItemText = item.SubItems[column].Text;

            // Use the initial letter instead if it is the first column.
            if (column == 0) 
            {
                subItemText = subItemText.Substring(0, 1);
            }

            // If the groups table does not already contain a group
            // for the subItemText value, add a new group using the 
            // subItemText value for the group header and Hashtable key.
            if (!groups.Contains(subItemText))
            {
                groups.Add( subItemText, new ListViewGroup(subItemText, 
                    HorizontalAlignment.Left) );
            }
        }

        // Return the Hashtable object.
        return groups;
    }

    // Sorts ListViewGroup objects by header value.
    private class ListViewGroupSorter : IComparer
    {
        private SortOrder order;

        // Stores the sort order.
        public ListViewGroupSorter(SortOrder theOrder) 
        { 
            order = theOrder;
        }

        // Compares the groups by header value, using the saved sort
        // order to return the correct value.
        public int Compare(object x, object y)
        {
            int result = String.Compare(
                ((ListViewGroup)x).Header,
                ((ListViewGroup)y).Header
            );
            if (order == SortOrder.Ascending)
            {
                return result;
            }
            else 
            {
                return -result;
            }
        }
    }

}
Imports System.Collections
Imports System.Windows.Forms

Public Class ListViewGroupsExample
    Inherits Form

    Private myListView As ListView

    ' Determine whether Windows XP or a later
    ' operating system is present.
    Private isRunningXPOrLater As Boolean = _
        OSFeature.Feature.IsPresent(OSFeature.Themes)
    
    ' Declare a Hashtable array in which to store the groups.
    Private groupTables() As Hashtable
    
    ' Declare a variable to store the current grouping column.
    Private groupColumn As Integer = 0
    
    Public Sub New()
        ' Initialize myListView.
        myListView = New ListView()
        myListView.Dock = DockStyle.Fill
        myListView.View = View.Details
        myListView.Sorting = SortOrder.Ascending
        
        ' Create and initialize column headers for myListView.
        Dim columnHeader0 As New ColumnHeader()
        columnHeader0.Text = "Title"
        columnHeader0.Width = -1
        Dim columnHeader1 As New ColumnHeader()
        columnHeader1.Text = "Author"
        columnHeader1.Width = -1
        Dim columnHeader2 As New ColumnHeader()
        columnHeader2.Text = "Year"
        columnHeader2.Width = -1
        
        ' Add the column headers to myListView.
        myListView.Columns.AddRange( New ColumnHeader() _
            {columnHeader0, columnHeader1, columnHeader2} )
        
        ' Add a handler for the ColumnClick event.
        AddHandler myListView.ColumnClick, AddressOf myListView_ColumnClick
        
        ' Create items and add them to myListView.
        Dim item0 As New ListViewItem( New String() _
            {"Programming Windows", _
            "Petzold, Charles", _
            "1998"} )
        Dim item1 As New ListViewItem( New String() _
            {"Code: The Hidden Language of Computer Hardware and Software", _
            "Petzold, Charles", _
            "2000"} )
        Dim item2 As New ListViewItem( New String() _
            {"Programming Windows with C#", _
            "Petzold, Charles", _
            "2001"} )
        Dim item3 As New ListViewItem( New String() _
            {"Coding Techniques for Microsoft Visual Basic .NET", _
            "Connell, John", _
            "2001"} )
        Dim item4 As New ListViewItem( New String() _
            {"C# for Java Developers", _
            "Jones, Allen / Freeman, Adam", _
            "2002"} )
        Dim item5 As New ListViewItem( New String() _
            {"Microsoft .NET XML Web Services Step by Step", _
            "Jones, Allen / Freeman, Adam", _
            "2002"} )
        myListView.Items.AddRange( _
            New ListViewItem() {item0, item1, item2, item3, item4, item5})
        
        If isRunningXPOrLater
            ' Create the groupsTable array and populate it with one 
            ' hash table for each column.
            groupTables = New Hashtable(myListView.Columns.Count) {}
            Dim column As Integer
            For column = 0 To myListView.Columns.Count - 1
                ' Create a hash table containing all the groups 
                ' needed for a single column.
                groupTables(column) = CreateGroupsTable(column)
            Next column
            
            ' Start with the groups created for the Title column.
            SetGroups(0)
        End If
        
        ' Initialize the form.
        Me.Controls.Add(myListView)
        Me.Size = New System.Drawing.Size(550, 330)
        Me.Text = "ListView Groups Example"
    End Sub
    
    <STAThread()> _
    Shared Sub Main()
        Application.EnableVisualStyles()
        Application.Run(New ListViewGroupsExample())
    End Sub
    
    ' Groups the items using the groups created for the clicked 
    ' column.
    Private Sub myListView_ColumnClick( _
        sender As Object, e As ColumnClickEventArgs)

        ' Set the sort order to ascending when changing
        ' column groups; otherwise, reverse the sort order.
        If myListView.Sorting = SortOrder.Descending OrElse _
            isRunningXPOrLater And e.Column <> groupColumn Then
            myListView.Sorting = SortOrder.Ascending
        Else
            myListView.Sorting = SortOrder.Descending
        End If
        groupColumn = e.Column
        
        ' Set the groups to those created for the clicked column.
        If isRunningXPOrLater Then
            SetGroups(e.Column)
        End If
    End Sub
    
    ' 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

    ' Creates a Hashtable object with one entry for each unique
    ' subitem value (or initial letter for the parent item)
    ' in the specified column.
    Private Function CreateGroupsTable(column As Integer) As Hashtable
        ' Create a Hashtable object.
        Dim groups As New Hashtable()
        
        ' Iterate through the items in myListView.
        Dim item As ListViewItem
        For Each item In myListView.Items
            ' Retrieve the text value for the column.
            Dim subItemText As String = item.SubItems(column).Text
            
            ' Use the initial letter instead if it is the first column.
            If column = 0 Then
                subItemText = subItemText.Substring(0, 1)
            End If 

            ' If the groups table does not already contain a group
            ' for the subItemText value, add a new group using the 
            ' subItemText value for the group header and Hashtable key.
            If Not groups.Contains(subItemText) Then
                groups.Add( subItemText, New ListViewGroup(subItemText, _
                    HorizontalAlignment.Left) )
            End If
        Next item
        
        ' Return the Hashtable object.
        Return groups
    End Function 'CreateGroupsTable

    ' Sorts ListViewGroup objects by header value.
    Private Class ListViewGroupSorter
        Implements IComparer 
        
        Private order As SortOrder
        
        ' Stores the sort order.
        Public Sub New(theOrder As SortOrder)
            order = theOrder
        End Sub
        
        ' Compares the groups by header value, using the saved sort
        ' order to return the correct value.
        Public Function Compare(x As Object, y As Object) As Integer _
            Implements IComparer.Compare
            Dim result As Integer = String.Compare( _
                CType(x, ListViewGroup).Header, _
                CType(y, ListViewGroup).Header )
            If order = SortOrder.Ascending Then
                Return result
            Else
                Return -result
            End If
        End Function 'Compare
    End Class 'ListViewGroupSorter 

End Class 'ListViewGroupsExample

Hinweise

Mit ListView der Gruppierungs Funktion können Sie visuelle Gruppen von ListView logisch verknüpften Elementen erstellen.The ListView grouping feature lets you create visual groups of logically related ListView items. Jede Gruppe besteht aus einem textbasierten Header, gefolgt von einer horizontalen Linie und den Elementen, die dieser Gruppe zugewiesen sind.Each group consists of a text-based header followed by a horizontal line and the items assigned to that group. Sie können den Header Text Links, rechts oder zentriert im Steuerelement ausrichten.You can align the header text to the left, right, or center of the control. Alle einem ListView Steuerelement zugewiesenen Gruppen werden immer dann ListView.View angezeigt, wenn die-Eigenschaft auf einen View.Listanderen Wert als festgelegt ist.Any groups assigned to a ListView control appear whenever the ListView.View property is set to a value other than View.List.

ListViewmithilfe von Gruppen können Ihre Benutzer nach den gesuchten Elementen suchen, indem Sie die Elemente in nützliche Kategorien aufteilen.ListView groups help your users find the items they are looking for by separating the items into useful categories. Sie können alle benötigten Kategorien erstellen.You can create whatever categories you need. Eine typische Methode zum Gruppieren der Elemente ist die Art und Weise, wie Sie sortiert werden.One typical way to group the items is by the way they are sorted. Beispielsweise können Sie die Elemente nach dem ersten Buchstaben des Element namens gruppieren, wenn diese alphabetisch sortiert sind, oder durch unter Elemente wie Typ oder Datum, wenn die Liste sortiert wird, indem Sie in der Detailansicht auf einen Spaltenheader klicken.For example, you can group the items by the initial letter of the item name when they are sorted alphabetically, or by subitems such as type or date when the list is sorted by clicking a column header in the details view. Windows-Explorer verwendet diesen Gruppierungstyp.Windows Explorer uses this type of grouping.

Um die Gruppierungs Funktion zu verwenden, fügen Sie ListViewGroup der ListView.Groups -Auflistung eines ListView -Steuer Elements ein oder mehrere-Objekte hinzu.To use the grouping feature, add one or more ListViewGroup objects to the ListView.Groups collection of a ListView control. Legen Sie den Gruppen Header Text und die Header Ausrichtung ListViewGroup im Konstruktor fest, oder legen Sie Header diese HeaderAlignment mithilfe der-Eigenschaft und der-Eigenschaft fest.Set the group header text and header alignment in the ListViewGroup constructor, or set them using the Header and HeaderAlignment properties.

Sie können einer Gruppe ein Element zuweisen, indem Sie die Gruppe im ListViewItem Konstruktor angeben, indem Sie die ListViewItem.Group -Eigenschaft festlegen oder das Element direkt der Items -Auflistung einer-Gruppe hinzufügen.You can assign an item to a group by specifying the group in the ListViewItem constructor, by setting the ListViewItem.Group property, or by directly adding the item to the Items collection of a group. Alle Elemente sollten Gruppen zugewiesen werden, bevor Sie angezeigt werden.All items should be assigned to groups before they are displayed. Alle Elemente, die keiner Gruppe zugewiesen sind, werden in der Standardgruppe mit der Header Bezeichnung "DefaultGroup{0}" angezeigt.Any items that are not assigned to a group will appear in the default group, which has the header label "DefaultGroup{0}". Die Standardgruppe ist nicht in der ListView.Groups Sammlung enthalten und kann nicht geändert werden.The default group is not contained in the ListView.Groups collection, and cannot be altered. Dies ist in erster Linie hilfreich beim Debuggen, um sicherzustellen, dass alle Elemente ordnungsgemäß zu Gruppen hinzugefügt wurdenIt is primarily useful in debugging to ensure that all items have been properly added to groups.

Ein Element kann jeweils nur in einer Gruppe gleichzeitig sein.An item can only be in one group at a time. Sie können die Gruppe, zu der ein Element gehört, ändern, ListViewItem.Group indem Sie die Eigenschaft zur Laufzeit festlegen oder Sie der Items Auflistung einer anderen Gruppe hinzufügen, wodurch Sie automatisch aus der vorherigen Gruppe entfernt wird.You can change the group to which an item belongs by setting the ListViewItem.Group property at run time or by adding it to the Items collection of another group, which automatically removes it from the previous group.

Hinweis

Die Funktion "Einfügemarke" ist beim Verwenden von Gruppen deaktiviert.The insertion mark feature is disabled when using groups. Dies liegt daran, dass die Gruppierungs Funktion Elemente nach Gruppenmitgliedschaften sortiert, während die Funktion für die Einfügemarke mit Drag & Drop ListView -Neupositionierung in einem Steuerelement verwendet wird, das ihre Elemente nicht bereits sortiert.This is because the grouping feature orders items by group membership, while the insertion mark feature is used with drag-and-drop repositioning in a ListView control that does not already order its items.

Hinweis

ListViewGruppen sind nur unter Windows XP und der Windows Server 2003-Familie verfügbar, wenn die Anwendung Application.EnableVisualStyles die-Methode aufruft.ListView groups are available only on Windows XP and the Windows Server 2003 family when your application calls the Application.EnableVisualStyles method. Unter früheren Betriebssystemen wird jeglicher Code, der sich auf Gruppen bezieht, ignoriert, und die Gruppen werden nicht angezeigt.On earlier operating systems, any code relating to groups will be ignored and the groups will not appear. Folglich funktioniert jeglicher Code, der von der Gruppierungs Funktion abhängt, möglicherweise nicht ordnungsgemäß.As a result, any code that depends on the grouping feature might not work correctly. Möglicherweise möchten Sie einen Test einschließen, der bestimmt, ob die Gruppierungs Funktion verfügbar ist, und alternative Funktionen bereitstellen, wenn Sie nicht verfügbar sind.You might want to include a test that determines whether the grouping feature is available, and provide alternate functionality when it is unavailable. Sie können z. b. bei der Ausführung unter Betriebssystemen, die keine Sortierung nach Gruppe unterstützen, eine alternative Sortierung bereitstellen.For example, you might want to provide alternate sorting when running on operating systems that do not support sorting by group.

Die Funktion "Einfügemarke" wird von derselben Bibliothek bereitgestellt, die das Feature "Betriebssystem Designs" enthält.The insertion mark feature is provided by the same library that provides the operating system themes feature. Um die Verfügbarkeit dieser Bibliothek zu überprüfen, müssen FeatureSupport.IsPresent(Object) Sie die-Methoden Überladung OSFeature.Themes aufrufen und den-Wert übergeben.To check for the availability of this library, call the FeatureSupport.IsPresent(Object) method overload and pass in the OSFeature.Themes value.

Konstruktoren

ListViewGroup() ListViewGroup() ListViewGroup() ListViewGroup()

Initialisiert eine neue Instanz der ListViewGroup-Klasse unter Verwendung des Standardheadertextes "ListViewGroup" und der Standardheaderausrichtung (linksbündig).Initializes a new instance of the ListViewGroup class using the default header text of "ListViewGroup" and the default left header alignment.

ListViewGroup(String) ListViewGroup(String) ListViewGroup(String) ListViewGroup(String)

Initialisiert eine neue Instanz der ListViewGroup-Klasse unter Verwendung des angegebenen Werts zum Initialisieren der Header-Eigenschaft und unter Verwendung der linksbündigen Standardheaderausrichtung.Initializes a new instance of the ListViewGroup class using the specified value to initialize the Header property and using the default left header alignment.

ListViewGroup(String, HorizontalAlignment) ListViewGroup(String, HorizontalAlignment) ListViewGroup(String, HorizontalAlignment) ListViewGroup(String, HorizontalAlignment)

Initialisiert eine neue Instanz der ListViewGroup-Klasse unter Verwendung des angegebenen Headertextes "ListViewGroup" und der angegebenen Headerausrichtung.Initializes a new instance of the ListViewGroup class using the specified header text and the specified header alignment.

ListViewGroup(String, String) ListViewGroup(String, String) ListViewGroup(String, String) ListViewGroup(String, String)

Initialisiert eine neue Instanz der ListViewGroup-Klasse unter Verwendung der angegebenen Werte zum Initialisieren der Name-Eigenschaft und der Header-Eigenschaft.Initializes a new instance of the ListViewGroup class using the specified values to initialize the Name and Header properties.

Eigenschaften

Header Header Header Header

Ruft den Headertext für die Gruppe ab oder legt diesen fest.Gets or sets the header text for the group.

HeaderAlignment HeaderAlignment HeaderAlignment HeaderAlignment

Ruft die Ausrichtung des Gruppenheadertextes ab oder legt diesen fest.Gets or sets the alignment of the group header text.

Items Items Items Items

Ruft eine Auflistung mit allen Elementen ab, die dieser Gruppe zugeordnet sind.Gets a collection containing all items associated with this group.

ListView ListView ListView ListView

Ruft das ListView-Steuerelement ab, das diese Gruppe enthält.Gets the ListView control that contains this group.

Name Name Name Name

Ruft den Namen der Gruppe ab oder legt diesen fest.Gets or sets the name of the group.

Tag Tag Tag Tag

Ruft das Objekt ab, das Daten über die Gruppe enthält, oder legt dieses fest.Gets or sets the object that contains data about the group.

Methoden

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

Bestimmt, ob das angegebene Objekt mit dem aktuellen Objekt identisch ist.Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

Fungiert als Standardhashfunktion.Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

Ruft den Type der aktuellen Instanz ab.Gets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

Erstellt eine flache Kopie des aktuellen Object.Creates a shallow copy of the current Object.

(Inherited from Object)
ToString() ToString() ToString() ToString()

Gibt eine Zeichenfolge zurück, die das aktuelle Objekt darstellt.Returns a string that represents the current object.

Explizite Schnittstellenimplementierungen

ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext) ISerializable.GetObjectData(SerializationInfo, StreamingContext)

Füllt eine SerializationInfo mit den Daten auf, die zum Serialisieren des Zielobjekts erforderlich sind.Populates a SerializationInfo with the data needed to serialize the target object.

Gilt für:

Siehe auch