Example of Using String.Format to Format Strings in XAML in WPF

Example of Using String.Format to Format Strings in XAML in WPF

String Formatting

Last updated 5/22/2022 9:50 PM
眾尋
3 min read
Category
WPF
Tags
.NET WPF
  1. Currency Format
<TextBlock Text="{Binding Price, StringFormat={}{0:C}}" /> // $123.46
  1. Currency Format, One Decimal Place
<TextBox Text="{Binding Price, StringFormat={}{0:C1}}" /> // $123.5
  1. Prefix Text
<TextBox Text="{Binding Price, StringFormat=Unit Price: {0:C}}" /> // Unit Price: $123.46
  1. Suffix Text
<TextBox Text="{Binding Price, StringFormat={}{0} Yuan}" /> // 123.45678 Yuan
  1. Fixed Number of Digits (Minimum digits, only supports integers)
<TextBox Text="{Binding Count, StringFormat={}{0:D6}}" /> // 086723
  1. Specify Number of Decimal Places
<TextBox Text="{Binding Total, StringFormat={}{0:F4}}" /> // 28768234.9329
  1. Number Grouped with Commas and Specify Decimal Places
<TextBox Text="{Binding Total, StringFormat={}{0:N3}}" /> // 28,768,234.933
  1. Format Percentage
<TextBox Text="{Binding Persent, StringFormat={}{0:P1}}" /> // 78.9 %
  1. Placeholder
<TextBox Text="{Binding Price, StringFormat={}{0:0000.00}}" /> // 0123.46
<TextBox Text="{Binding Price, StringFormat={}{0:####.##}}" /> // 123.46
  1. Date/Time
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:d}}" /> // 5/4/2015
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:D}}" /> // Monday, May 04, 2015
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:f}}" /> // Monday, May 04, 2015 5:46 PM
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:F}}" /> // Monday, May 04, 2015 5:46:56 PM
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:g}}" /> // 5/4/2015 5:46 PM
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:G}}" /> // 5/4/2015 5:46:56 PM
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:m}}" /> // May 04
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:M}}" /> // May 04
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:t}}" /> // 5:46 PM
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:T}}" /> // 5:46:56 PM
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:yyyy'年'MM'月'dd'日'}}" /> // 2015年05月04日
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:yyyy-MM-dd}}" /> // 2015-05-04
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:yyyy-MM-dd HH:mm}}" /> // 2015-05-04 17:46
<TextBox Text="{Binding DateTimeNow, StringFormat={}{0:yyyy-MM-dd HH:mm:ss}}" /> // 2015-05-04 17:46:56

Note: The Chinese version used literal Chinese characters in the format string. In English, you can still use the same literal approach as shown above. Alternatively, you can use a custom format string like {0:yyyy'年'MM'月'dd'日'} to preserve the Chinese characters if needed. The translation follows the original intent.

  1. MultiBinding
<TextBox.Text>
    <MultiBinding StringFormat="Name: {0}{1}">
         <Binding Path="FristName" />
         <Binding Path="LastName" />
    </MultiBinding>
 </TextBox.Text>
// Name: AAbb
  1. Special Characters in MultiBinding
<TextBox.Text>
     <MultiBinding StringFormat="Name: {0}&#x09;{1}">
           <Binding Path="FristName" />
           <Binding Path="LastName" />
     </MultiBinding>
 </TextBox.Text>

<!--
\a  &#x07;  BEL
\b  &#x08;  BS - Backspace
\f  &#x0c;  FF - Formfeed
\n  &#x0a;  LF, NL - Linefeed, New Line
\r  &#x0d;  CR - Carriage return
\t  &#x09;  HT - Tab, Horizontal Tabelator
\v  &#x0b;  VT - Vertical Tabelator
-->

Original author: Zhong Xun

Original link: https://www.cnblogs.com/ZXdeveloper/p/15513657.html

Keep Exploring

Related Reading

More Articles
Same category / Same tag 9/13/2025

Migration Series from WPF to Avalonia: Why I Must Migrate My WPF Application to Avalonia

In the past few years, our host computer software has mainly been developed using WPF and WinForm . These technologies work well on the Windows platform and have accompanied us from small-scale trial production to the current stage of large-scale delivery. However, with business development and changes in customer requirements, the single Windows technology stack has gradually become a hurdle we must overcome.

Continue Reading
Same category / Same tag 1/26/2025

Implementing Internationalization in WPF Using Custom XML Files

This article details the method of implementing internationalization in WPF applications using custom XML files, including installing the necessary NuGet packages, dynamically retrieving the language list, dynamically switching languages, using translated strings in code and XAML interfaces, and provides a source code link to help developers easily achieve internationalization in WPF applications.

Continue Reading