GroupStyle.HeaderStringFormat プロパティ

定義

ヘッダーが文字列として表示される場合に、その書式を指定する複合文字列を取得または設定します。

public:
 property System::String ^ HeaderStringFormat { System::String ^ get(); void set(System::String ^ value); };
public string HeaderStringFormat { get; set; }
member this.HeaderStringFormat : string with get, set
Public Property HeaderStringFormat As String

プロパティ値

ヘッダーが文字列として表示される場合に、その書式を指定する複合文字列。

次の例では、 ListBox 価格範囲別に品目のコレクションをグループ化する を作成します。 コンバーター は、 GroupByPrice各項目の価格を上回る整数値を返します。 たとえば、品目の価格が 0 ~ 100 の場合、コンバーターは 100 を返します。 この例では、 を HeaderStringFormat 使用して、各価格範囲の上限を含む文字列を作成します。 たとえば、米国では、100 ドル以下のアイテムは、"$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>

次の例は、 GroupByPrice がバインドされているListBoxクラスPurchaseItem、クラス、およびコレクションを示しています。

// 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))));
    }
}
' The converter to group the items.
Public Class GroupByPrice
    Implements IValueConverter
    Public Function Convert(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.Convert

        If Not(TypeOf value Is Double) Then
            Return Nothing
        End If

        Dim itemPrice As Double = CDbl(value)

        If itemPrice < 100 Then
            Return 100
        End If

        If itemPrice < 250 Then
            Return 250
        End If

        If itemPrice < 500 Then
            Return 500
        End If

        Return 1000



    End Function

    Public Function ConvertBack(ByVal value As Object, ByVal targetType As Type, ByVal parameter As Object, ByVal culture As System.Globalization.CultureInfo) As Object Implements IValueConverter.ConvertBack
        Throw New NotImplementedException()
    End Function
End Class

' The type of objects that are added to the ItemsControl.
Public Class PurchaseItem
    Public Property Description() As String
    Public Property Price() As Double
    Public Property OfferExpires() As Date

    Public Sub New()
    End Sub

    Public Sub New(ByVal desc As String, ByVal price As Double, ByVal endDate As Date)
        Description = desc
        Me.Price = price
        OfferExpires = endDate
    End Sub

    Public Overrides Function ToString() As String
        Return String.Format("{0}, {1:c}, {2:D}", Description, Price, OfferExpires)
    End Function
End Class

' The source of the ItemsControl.
Public Class ItemsForSale
    Inherits ObservableCollection(Of PurchaseItem)

    Public Sub New()
        Add((New PurchaseItem("Snowboard and bindings", 120, New Date(2009, 1, 1))))
        Add((New PurchaseItem("Inside VB, second edition", 10, New Date(2009, 2, 2))))
        Add((New PurchaseItem("Laptop - only 1 year old", 499.99, New Date(2009, 2, 28))))
        Add((New PurchaseItem("Set of 6 chairs", 120, New Date(2009, 2, 28))))
        Add((New PurchaseItem("My DVD Collection", 15, New Date(2009, 1, 1))))
        Add((New PurchaseItem("TV Drama Series", 39.985, New Date(2009, 1, 1))))
        Add((New PurchaseItem("Squash racket", 60, New Date(2009, 2, 28))))

    End Sub

End Class

注釈

HeaderStringFormat には、定義済み、複合、またはカスタムの文字列形式を指定できます。 文字列形式の詳細については、「 型の書式設定」を参照してください。 の または HeaderTemplateSelector プロパティをHeaderTemplateGroupStyle設定した場合、HeaderStringFormatプロパティは無視されます。

適用対象