Editar

Compartir a través de


BindingBase.StringFormat Property

Definition

Gets or sets a string that specifies how to format the binding if it displays the bound value as a string.

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

Property Value

A string that specifies how to format the binding if it displays the bound value as a string.

Examples

The following example uses the StringFormat property to convert Price, which is a Double, to a string that represents a currency.

<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>

The following example uses the StringFormat property on a MultiBinding to build a string that includes the Description and Price of each item in a ListBox.

<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>

Remarks

StringFormat can be a predefined, composite, or custom string format. For more information about string formats, see Formatting Types.

If you set the Converter and StringFormat properties, the converter is applied to the data value first, and then the StringFormat is applied.

When you set the StringFormat on a Binding to a composite string format, you can specify only one parameter.

When you use a MultiBinding, the StringFormat property applies only when it is set on the MultiBinding. The value of StringFormat that is set on any child Binding objects is ignored. The number of parameters in a composite string format cannot exceed the number of child Binding objects in the MultiBinding.

When you use a PriorityBinding, you can set the StringFormat on the PriorityBinding, on child binding objects, or both. If the StringFormat is set on the child binding that is applied, that value is used. If the StringFormat is not set on the child binding that is applied, the StringFormat of the PriorityBinding is applied if it is set.

If the format string starts with the { character, the XAML parser will confuse it for a markup extension. To avoid this ambiguity, prefix the format string with an empty set of curly braces.

Applies to