TabControl.ContentStringFormat 屬性
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
會取得一個複合字串,指定如果物件以字串顯示,該如何格式化內容 TabItem 。
public:
property System::String ^ ContentStringFormat { System::String ^ get(); void set(System::String ^ value); };
public string ContentStringFormat { get; set; }
member this.ContentStringFormat : string with get, set
Public Property ContentStringFormat As String
屬性值
一個複合字串,指定若物件以字串顯示,該如何格式化其內容 TabItem 。
範例
以下範例將 a TabControl 綁定為一組 Student 物件。 該 Student 類別有一個 Name 屬性,即一組 Course 物件,並實作 IFormattable.ToString 回傳學生的 或 Name 列出學生課程的字串的方法。 範例中 將HeaderedContentControl.HeaderStringFormat學生姓名置於 HeaderTabItem 的 中(繼承自 HeaderedContentControl),並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
備註
ContentStringFormat 可以是預先定義的、複合的或自訂的字串格式。 欲了解更多字串格式資訊,請參閱 格式類型。 如果你設定 ContentTemplateContentTemplateSelector 或 ,TabControlContentStringFormat則該屬性會被忽略。