Condividi tramite


TabControl.ContentStringFormat Proprietà

Definizione

Ottiene una stringa composita che specifica come formattare il contenuto degli TabItem oggetti se vengono 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à

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

Esempio

Nell'esempio seguente viene associato un oggetto TabControl a una raccolta di Student oggetti . La Student classe ha una Name proprietà, una raccolta di Course oggetti e implementa il IFormattable.ToString metodo per restituire l'oggetto Name dello studente o una stringa che elenca i corsi dello studente. Nell'esempio viene HeaderedContentControl.HeaderStringFormat usato per inserire il nome di uno studente in Header di ogni TabItem oggetto (che eredita da HeaderedContentControl) e per ContentStringFormat visualizzare l'elenco dei corsi per ogni 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 l'oggetto Name dello studente o una stringa che elenca i 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 di stringa predefinito, composito o personalizzato. Per altre informazioni sui formati stringa, vedere Formattazione dei tipi. Se si imposta o ContentTemplateContentTemplateSelector di un TabControloggetto , la ContentStringFormat proprietà viene ignorata.

Si applica a