HeaderedContentControl.HeaderStringFormat 屬性

定義

取得或設定複合字串,這個字串會指定在 Header 屬性顯示為字串的情況下,如何格式化這個屬性。

public:
 property System::String ^ HeaderStringFormat { System::String ^ get(); void set(System::String ^ value); };
[System.ComponentModel.Bindable(true)]
public string HeaderStringFormat { get; set; }
[<System.ComponentModel.Bindable(true)>]
member this.HeaderStringFormat : string with get, set
Public Property HeaderStringFormat As String

屬性值

複合字串,指定在 Header 屬性顯示為字串的情況下,如何格式化這個屬性。 預設為 null

屬性

範例

下列範例會將 系 TabControl 結至 物件的集合 Student 。 類別 Student 具有 Name 屬性、物件的集合 Course ,並實 IFormattable.ToString 作 方法,以傳回 Name 學生的 或列出學生課程的字串。 此範例會使用 HeaderStringFormat 將學生的名稱 Header 放在繼承自 HeaderedContentControl) 的每個 TabItem (中,以及在 TabControl.ContentStringFormat 的內容中 TabItem 顯示每個學生的課程清單。

<Grid>
  <Grid.Resources>
    <src:Students x:Key="Students"/>

    <Style TargetType="TabItem">
      <Setter Property="HeaderStringFormat" Value="n"/>
      <Setter Property="FontFamily" Value="Lucida Sans Unicode"/>
      <Setter Property="Foreground" Value="Green"/>
      <Setter Property="FontWeight" Value="Bold"/>
    </Style>
  </Grid.Resources>

  <TabControl ItemsSource="{StaticResource Students}"
              FontFamily="Lucida Console" Foreground="Navy"
              ContentStringFormat="cl"/>
</Grid>

下列範例會實作 IFormattable.ToString 方法,以傳回 Name 學生或列出學生課程的字串。

public string ToString(string format, IFormatProvider formatProvider)
{
    // 'n': print the name only.
    if (format == "n")
    {
        return Name;
    }

    // 'cl': print the course list.
    if (format == "cl")
    {
        string stringFormat = "{0,-25}{1,-30}{2,-10}\r\n";

        StringBuilder str = new StringBuilder();

        str.AppendLine(); 
        str.AppendFormat(stringFormat, "Title", "Description", "ID");
        str.AppendLine(); 

        foreach (Course c in Courses)
        {
            str.AppendFormat(stringFormat, c.Title, c.Description, c.SectionID);
        }

        return str.ToString();
    }

    return this.ToString();
}
Public Overloads Function ToString(ByVal format As String, ByVal formatProvider As IFormatProvider) As String Implements IFormattable.ToString
    ' 'n': print the name only.
    If format = "n" Then
        Return Name
    End If

    ' 'cl': print the course list.
    If format = "cl" Then
        Dim stringFormat As String = "{0,-25}{1,-30}{2,-10}" & vbCrLf

        Dim str As New StringBuilder()

        str.AppendLine()
        str.AppendFormat(stringFormat, "Title", "Description", "ID")
        str.AppendLine()

        For Each c As Course In Courses
            str.AppendFormat(stringFormat, c.Title, c.Description, c.SectionID)
        Next c

        Return str.ToString()
    End If

    Return Me.ToString()
End Function

備註

HeaderStringFormat 可以是預先定義的、複合或自訂字串格式。 如需字串格式的詳細資訊,請參閱 格式化類型。 如果您設定 HeaderTemplateHeaderedContentControlHeaderTemplateSelector 屬性,則會 HeaderStringFormat 忽略 屬性。

相依性屬性資訊

識別碼欄位 HeaderStringFormatProperty
設定為 的中繼資料屬性 true

適用於