GroupStyle Class

Defines how you want the group to look at each level.

System.Object
  System.Windows.Controls.GroupStyle

Namespace:  System.Windows.Controls
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

'Declaration
<LocalizabilityAttribute(LocalizationCategory.None, Readability := Readability.Unreadable)> _
Public Class GroupStyle _
	Implements INotifyPropertyChanged
<GroupStyle .../>

The GroupStyle type exposes the following members.

  NameDescription
Public methodGroupStyleInitializes a new instance of the GroupStyle class.
Top

  NameDescription
Public propertyAlternationCountGets or sets the number of alternating GroupItem objects.
Public propertyContainerStyleGets or sets the style that is applied to the GroupItem generated for each item.
Public propertyContainerStyleSelectorEnables the application writer to provide custom selection logic for a style to apply to each generated GroupItem.
Public propertyStatic memberDefaultGets the default style of the group.
Public propertyHeaderStringFormatGets or sets a composite string that specifies how to format the header if it is displayed as a string.
Public propertyHeaderTemplateGets or sets the template that is used to display the group header.
Public propertyHeaderTemplateSelectorEnables the application writer to provide custom selection logic for a template that is used to display the group header.
Public propertyHidesIfEmptyGets or sets a value that indicates whether items corresponding to empty groups should be displayed.
Public propertyPanelGets or sets a template that creates the panel used to layout the items.
Top

  NameDescription
Public methodEquals(Object)Determines whether the specified object is equal to the current object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as the default hash function. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Protected methodOnPropertyChangedRaises the PropertyChanged event using the provided arguments.
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Protected eventPropertyChangedOccurs when a property value changes.
Top

  NameDescription
Public fieldStatic memberDefaultGroupPanelIdentifies the default ItemsPanelTemplate that creates the panel used to layout the items.
Top

  NameDescription
Explicit interface implemetationPrivate eventINotifyPropertyChanged.PropertyChangedOccurs when a property value changes.
Top

The following examples show an ItemsControl that is bound to an XmlDataProvider and the code-behind content that contains the logic to add and remove grouping. When the check box is checked, the content of the ItemsControl is grouped by the Type attribute.

Each group is of type CollectionViewGroup. The GroupStyle HeaderTemplate is specified so that it appears as a TextBlock that displays the Name of each the group. In this case, the Name is either Work or Home.

<Window x:Class="GroupingSample.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Grouping Sample"
    Width="220" Height="550">
  <StackPanel>

    <StackPanel.Resources>
      <XmlDataProvider x:Key="myTasks" XPath="Tasks/Task">
        <x:XData>
          <Tasks xmlns="">
            <Task Name="Groceries" Priority="2" Type="Home">
              <Description>Pick up Groceries and Detergent</Description>
            </Task>
            <Task Name="Laundry" Priority="2" Type="Home">
              <Description>Do Laundry</Description>
            </Task>
            <Task Name="Email" Priority="1" Type="Work">
              <Description>Email Clients</Description>
            </Task>
            <Task Name="Clean" Priority="3" Type="Work">
              <Description>Clean my office</Description>
            </Task>
            <Task Name="Dinner" Priority="1" Type="Home">
              <Description>Get ready for family reunion</Description>
            </Task>
            <Task Name="Proposals" Priority="2" Type="Work">
              <Description>Review new budget proposals</Description>
            </Task>
          </Tasks>
        </x:XData>
      </XmlDataProvider>
    </StackPanel.Resources>

    <TextBlock Margin="12,5,5,0" FontSize="20" Text="My Task List"/>
    <CheckBox Margin="10,5,5,10" Checked="AddGrouping"
              Unchecked="RemoveGrouping">Group by task type</CheckBox>
    <ItemsControl Margin="10" Name="myItemsControl"
                  ItemsSource="{Binding Source={StaticResource myTasks}}">
      <ItemsControl.ItemTemplate>
        <DataTemplate>
          <DataTemplate.Resources>
            <Style TargetType="TextBlock">
              <Setter Property="FontSize" Value="18"/>
              <Setter Property="HorizontalAlignment" Value="Center"/>
            </Style>
          </DataTemplate.Resources>
          <Grid>
            <Ellipse Fill="Silver"/>
            <StackPanel>
              <TextBlock Margin="3,3,3,0"
                         Text="{Binding XPath=@Name}"/>
              <TextBlock Margin="3,0,3,7"
                         Text="{Binding XPath=@Priority}"/>
            </StackPanel>
          </Grid>
        </DataTemplate>
      </ItemsControl.ItemTemplate>
      <ItemsControl.ItemContainerStyle>
        <Style>
          <Setter Property="Control.Width" Value="100"/>
          <Setter Property="Control.Margin" Value="5"/>
        </Style>
      </ItemsControl.ItemContainerStyle>
      <ItemsControl.GroupStyle>
        <GroupStyle>
          <GroupStyle.HeaderTemplate>
            <DataTemplate>
              <TextBlock FontWeight="Bold" FontSize="15"
                         Text="{Binding Path=Name}"/>
            </DataTemplate>
          </GroupStyle.HeaderTemplate>
        </GroupStyle>
      </ItemsControl.GroupStyle>
    </ItemsControl>
  </StackPanel>
</Window>

Imports System
Imports System.Windows
Imports System.Windows.Data

Namespace GroupingSample
	Partial Public Class Window1
		Inherits System.Windows.Window

		Public Sub New()
			InitializeComponent()
		End Sub

		Private myView As CollectionView
		Private Sub AddGrouping(ByVal sender As Object, ByVal e As RoutedEventArgs)
			myView = CType(CollectionViewSource.GetDefaultView(myItemsControl.ItemsSource), CollectionView)
			If myView.CanGroup = True Then
				Dim groupDescription As New PropertyGroupDescription("@Type")
				myView.GroupDescriptions.Add(groupDescription)
			Else
				Return
			End If
		End Sub

		Private Sub RemoveGrouping(ByVal sender As Object, ByVal e As RoutedEventArgs)
			myView = CType(CollectionViewSource.GetDefaultView(myItemsControl.ItemsSource), CollectionView)
			myView.GroupDescriptions.Clear()
		End Sub
	End Class
End Namespace

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft