String.Format options

No formatting

<TextBlock Grid.Row="1">No Formatting</TextBlock>
<TextBlock Grid.Row="1" Grid.Column="1"
 Text="{Binding ElementName=MyValue,Path=Value}"/>

Result:  No formatting 6.254789965422

 

Applying a Standard Format Specifier

<TextBlock Grid.Row="2">Currency</TextBlock>
<TextBlock Grid.Row="2" Grid.Column="1"
           Text="{Binding ElementName=MyValue,Path=Value,StringFormat='C'}"/>

Result: Currency $6.25

 

Adding a Prefix or Suffix

<TextBlock Grid.Row="3">More Text</TextBlock>
<TextBlock Grid.Row="3" Grid.Column="1"
           Text="{Binding ElementName=MyValue,Path=Value,StringFormat='Just {0:C} each'}"/>

Result: More Text Just $6.25 each

 

Using a Custom Picture Format

<TextBlock Grid.Row="4">Customised</TextBlock>
<TextBlock Grid.Row="4" Grid.Column="1"
           Text="{Binding ElementName=MyValue,Path=Value,
           StringFormat='Decreased by {0:0.0}%'}"/>
<TextBlock Grid.Row="5">Customised (without prefix)</TextBlock>
<TextBlock Grid.Row="5" Grid.Column="1"
           Text="{Binding ElementName=MyValue,Path=Value,
           StringFormat='{}{0:0.0}% decrease'}"/>
OR
<TextBlock Grid.Row="5" Grid.Column="1"
           Text="{Binding ElementName=MyValue,Path=Value,
           StringFormat='{0:#,#.00}% decrease'}"/>

Result:
Customised Decreased by 6.25%
Customised(without prefix) 6.25% decrease

 

Creating Culture-Specific Data Bindings

<TextBlock Grid.Row="6">German Culture</TextBlock>
<TextBlock Grid.Row="6" Grid.Column="1"
           Text="{Binding ElementName=MyValue,Path=Value,
                          StringFormat='{}{0:C}',ConverterCulture=de-DE}"/>
<TextBlock Grid.Row="7">Japanese Culture</TextBlock>
<TextBlock Grid.Row="7" Grid.Column="1"
           Text="{Binding ElementName=MyValue,Path=Value,
                          StringFormat='{}{0:C}',ConverterCulture='ja-JP'}"/>

Result:
German Culture 6,25€
Japanese Culture 6¥‎

 

 

 

Advertisements

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s