BindingBase.StringFormat Property

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

Namespace:  System.Windows.Data
Assembly:  PresentationFramework (in PresentationFramework.dll)
XMLNS for XAML: http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

public string StringFormat { get; set; }
<object StringFormat="string" .../>

Property Value

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

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.

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>


.NET Framework

Supported in: 4, 3.5 SP1, 3.0 SP2

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft