.NET Framework Class Library
BindingBase..::.StringFormat Property

Updated: July 2008

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
Syntax

Visual Basic (Declaration)
Public Property StringFormat As String
Visual Basic (Usage)
Dim instance As BindingBase
Dim value As String

value = instance.StringFormat

instance.StringFormat = value
C#
public string StringFormat { get; set; }
Visual C++
public:
property String^ StringFormat {
    String^ get ();
    void set (String^ value);
}
JScript
public function get StringFormat () : String
public function set StringFormat (value : String)
XAML Attribute Usage
<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.
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.

Examples

The following example uses the StringFormat property to convert Price, which is a Double, to a string that represents a currency. For the entire sample, see Formatting a String on a Binding Sample.

XAML
<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. For the entire sample, see Formatting a String on a Binding Sample.

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

Windows 7, Windows Vista, Windows XP SP2, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5 SP1, 3.0 SP2
See Also

Reference

Change History

Date

History

Reason

July 2008

Added topic for new member.

SP1 feature change.

Tags :


Community Content

Schneider
How do you set a string that contains a quote?
e.g. I have a String format like so:

"\"Hello {0}\""

And I would like the output to be:

"Hello John"

How can I accomplish this from within XAML?


Tags : question

Robert Turner
Using a format string with quotes in it

You can use single quotes to define the string (legal XML syntax) and then use the double quotes inside the format string. You do need to do this as an XML attribute of the XML element as opposed to within a binding string (where no quotes are used). There may be other techniques, but this should work.

Example:

If you are using a binding with string formatting, such as:

<TextBlock Text="{Binding Path=FirstName, StringFormat=Hello \{0\}}"/>

and you want to format the string as "Hello {0}" (e.g. "Hello John") [with the quotes] then you could change the XAML above as follows:

<TextBlock>
  <TextBlock.Text>
    <Binding Path="FirstName" StringFormat='"Hello {0}"' />
  </TextBlock.Text>
</TextBlock>

Note that in the first code block, I'm quoting the formatting braces, but not in the second code block, as they are not within a binding 'string definition' and do not need to be quoted.

Tags : answer

Page view tracker