Formatting text directly in XAML

August 4, 2010 | In Development |

You can format text strings directly in the XAML code of a WPF application.

For example, you can use standard .NET formatting such as currency and date:

<TextBlock Text="{Binding Amount, StringFormat={}{0:C}}"/>
<TextBlock Text="{Binding Date, StringFormat={}{0:dd/MM/yyyy}}"/>

The empty {} after the StringFormat= escapes the text before the formatting string.

You can add text in front of the formatting string too, e.g.
StringFormat=Amount: {0:C}
in which case you don’t need to escape the string with the {}.

More advanced formatting options can be acheived using a multi-binding, for example:

<TextBlock>
  <TextBlock.Text>
    <MultiBinding StringFormat="Date {0} Time {1}">
      <Binding Path="DueDate" />
      <Binding Path="DueTime" />
    </MultiBinding>
  </TextBlock.Text>
</TextBlock>

No Comments yet

TrackBack URI

Sorry, the comment form is closed at this time.

Login | XFN| WP
Powered by WordPress with cmb-web Theme design by Christian Bridge-Harrington.
Blog feed. Valid XHTML and CSS. ^Top^