GroupStyle.HeaderStringFormat Property

Updated: July 2008

Gets or sets a composite string that specifies how to format the header if it is displayed as a string.

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

public string HeaderStringFormat { get; set; }
<object HeaderStringFormat="string" .../>

Property Value

Type: System.String
A composite string that specifies how to format the header if it is displayed as a string.

HeaderStringFormat can be a predefined, composite, or custom string format. For more information about string formats, see Formatting Types. If you set the HeaderTemplate or HeaderTemplateSelector property of a GroupStyle, the HeaderStringFormat property is ignored.

The following example creates a ListBox that groups a collection of items by price range. The converter, GroupByPrice, returns an integer value that is above each item's price. For example, if an item's price is between 0 and 100, the converter returns 100. The example uses the HeaderStringFormat to create a string that includes the upper limit for each price range. For example, in the United States, items that are under 100 dollars are grouped under a heading that says "Items under $100".

<StackPanel>
  <StackPanel.Resources>

      <src:ItemsForSale x:Key="MyData"/>
      <src:GroupByPrice x:Key="GroupByPrice"/>

      <CollectionViewSource x:Key="PriceGroup" Source="{StaticResource MyData}">

        <CollectionViewSource.GroupDescriptions>
          <PropertyGroupDescription PropertyName="Price"
                                    Converter="{StaticResource GroupByPrice}"/>
      </CollectionViewSource.GroupDescriptions>
    </CollectionViewSource>
  </StackPanel.Resources>

  <ListBox ItemsSource="{Binding Source={StaticResource PriceGroup}}"
           DisplayMemberPath="Description">
    <ListBox.GroupStyle>
      <GroupStyle HeaderStringFormat="Items under {0:c}"/>
    </ListBox.GroupStyle>
  </ListBox>
</StackPanel>

The following example shows the GroupByPrice class, the PurchaseItem class, and the collection that the ListBox is bound to.

// The converter to group the items. 
public class GroupByPrice : IValueConverter
{
    public object Convert(object value,
                          Type targetType,
                          object parameter,
                          System.Globalization.CultureInfo culture)
    {

        if (!(value is double))
        {
            return null;
        }

        double itemPrice = (double)value;

        if (itemPrice < 100)
        {
            return 100;
        }

        if (itemPrice < 250)
        {
            return 250;
        }

        if (itemPrice < 500)
        {
            return 500;
        }

        return 1000;



    }

    public object ConvertBack(object value,
                              Type targetType,
                              object parameter,
                              System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

// The type of objects that are added to the ItemsControl. 
public class PurchaseItem 
{
    public string Description { get; set; }
    public double Price { get; set; }
    public DateTime OfferExpires { get; set; }

    public PurchaseItem()
    {
    }

    public PurchaseItem(string desc, double price, DateTime endDate)
    {
        Description = desc;
        Price = price;
        OfferExpires = endDate;
    }

    public override string ToString()
    {
        return String.Format("{0}, {1:c}, {2:D}", Description, Price, OfferExpires);
    }
}

// The source of the ItemsControl. 
public class ItemsForSale : ObservableCollection<PurchaseItem>
{

    public ItemsForSale()
    {
        Add((new PurchaseItem("Snowboard and bindings", 120, new DateTime(2009, 1, 1))));
        Add((new PurchaseItem("Inside C#, second edition", 10, new DateTime(2009, 2, 2))));
        Add((new PurchaseItem("Laptop - only 1 year old", 499.99, new DateTime(2009, 2, 28))));
        Add((new PurchaseItem("Set of 6 chairs", 120, new DateTime(2009, 2, 28))));
        Add((new PurchaseItem("My DVD Collection", 15, new DateTime(2009, 1, 1))));
        Add((new PurchaseItem("TV Drama Series", 39.985, new DateTime(2009, 1, 1))));
        Add((new PurchaseItem("Squash racket", 60, new DateTime(2009, 2, 28))));

    }

}

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5 SP1, 3.0 SP2

Date

History

Reason

July 2008

Added topic for new member.

SP1 feature change.

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft