Share via


TabControl.ContentStringFormat Proprietà

Definizione

Ottiene una stringa composita che specifica come formattare il contenuto degli oggetti TabItem se visualizzati come stringhe.

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

Valore della proprietà

String

Stringa composita che specifica come formattare il contenuto degli oggetti TabItem se visualizzati come stringhe.

Esempio

Nell'esempio seguente viene associato un oggetto TabControl a una raccolta di Student oggetti. La Student classe ha una proprietà, una Name raccolta di Course oggetti e implementa il IFormattable.ToString metodo per restituire l'oggetto dello studente o una stringa che elenca i Name corsi degli studenti. L'esempio usa HeaderedContentControl.HeaderStringFormat per inserire il nome di uno studente in Header ogni oggetto (che eredita da HeaderedContentControl) e ContentStringFormat per visualizzare l'elenco dei corsi per ogni TabItem studente nel contenuto di 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>

Nell'esempio seguente viene implementato il IFormattable.ToString metodo per restituire lo studente o una stringa che elenca i Name corsi degli studenti.

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

Commenti

ContentStringFormat può essere un formato stringa predefinito, composito o personalizzato. Per altre informazioni sui formati di stringa, vedere Tipi di formattazione. Se si imposta o ContentTemplate ContentTemplateSelector di un TabControloggetto , la ContentStringFormat proprietà viene ignorata.

Si applica a