ContentControl.ContentStringFormat 屬性

定義

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

C#
[System.ComponentModel.Bindable(true)]
public string ContentStringFormat { get; set; }

屬性值

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

屬性

範例

下列範例會藉由在 中設定 屬性,並將 套用 Style 至繼承自 ContentControl 的兩 Label 個 物件,以指定 物件的格式 DateTimeContentStringFormatStyle

XAML
<StackPanel
  xmlns:sys="clr-namespace:System;assembly=mscorlib">

  <StackPanel.Resources>
    <Style x:Key="DateLabel" TargetType="Label">
      <Setter Property="ContentStringFormat" Value="dddd – d - MMMM"/>
    </Style>
  </StackPanel.Resources>

  <!--The results when the default culture is en-US
      is March 4, 2005.-->
  <Label Style="{StaticResource DateLabel}">
    <sys:DateTime>2004/3/4 13:6:55</sys:DateTime>
  </Label>

  <Label Style="{StaticResource DateLabel}">
    <x:Static Member="sys:DateTime.Now"/>
  </Label>

</StackPanel>

備註

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

適用於

產品 版本
.NET Framework 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
Windows Desktop 3.0, 3.1, 5, 6, 7, 8, 9, 10