通过


BindingBase.StringFormat 属性

定义

获取或设置一个字符串,该值指定在绑定将绑定值显示为字符串时如何设置绑定的格式。

public:
 property System::String ^ StringFormat { System::String ^ get(); void set(System::String ^ value); };
public string StringFormat { get; set; }
member this.StringFormat : string with get, set
Public Property StringFormat As String

属性值

一个字符串,指定在绑定将绑定值显示为字符串时如何设置绑定格式。

示例

下面的示例使用 StringFormat 属性将一个表示货币的字符串转换为 Price一个 Double字符串。

<ListView ItemsSource="{StaticResource MyData}">
  <ListView.View>
    <GridView>
      <GridViewColumn DisplayMemberBinding="{Binding Path=Description}"/>
      <GridViewColumn DisplayMemberBinding="{Binding Path=Price, StringFormat=Now {0:c}!}"/>
    </GridView>
  </ListView.View>
</ListView>

以下示例使用 StringFormat 属性在 a MultiBinding 上生成一个字符串,该字符串包含 Description 每个项和 PriceListBox

<ListBox ItemsSource="{StaticResource MyData}">

  <ListBox.ItemTemplate>
    <DataTemplate>
      <TextBlock>
        <TextBlock.Text>
          <MultiBinding  StringFormat="{}{0} -- Now only {1:C}!">
            <Binding Path="Description"/>
            <Binding Path="Price"/>
          </MultiBinding>
        </TextBlock.Text>
      </TextBlock>
    </DataTemplate>
  </ListBox.ItemTemplate>
</ListBox>

注解

StringFormat 可以是预定义、复合或自定义字符串格式。 有关字符串格式的详细信息,请参阅 格式设置类型

如果设置 ConverterStringFormat 属性,则首先将转换器应用于数据值,然后 StringFormat 应用该转换器。

StringFormat将 on Binding 设置为复合字符串格式时,只能指定一个参数。

使用 a MultiBinding时,该 StringFormat 属性仅在设置属性 MultiBinding时适用。 忽略在任何子Binding对象上设置的值StringFormat。 复合字符串格式的参数数不能超过 中的MultiBindingBinding对象数。

使用 a PriorityBinding时,可以在子绑定对象上或同时设置 StringFormat on PriorityBinding和/或子绑定对象。 如果在应用的子绑定上设置该值 StringFormat ,则使用该值。 如果未在应用的子绑定上设置, StringFormatStringFormatPriorityBinding 则设置子绑定时将应用该绑定。

如果格式字符串以 { 字符开头,XAML 分析器会将它与标记扩展混淆。 为了避免这种歧义,请为格式字符串添加一组空大括号的前缀。

适用于