Example of using String.Format to format a string in XAML in WPF

Example of using String.Format to format a string in XAML in WPF

string formatting

最后更新 5/22/2022 9:50 PM
眾尋
预计阅读 3 分钟
分类
WPF
标签
.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. preliterate
<TextBox Text="{Binding Price, StringFormat=单价:{0:C}}" /> //单价:$123.46
  1. posttext
<TextBox Text="{Binding Price, StringFormat={}{0}元}" /> // 123.45678元
  1. Fixed number of bits, and the number of bits cannot be less than before formatting. Only integer numbers are supported
<TextBox Text="{Binding Count, StringFormat={}{0:D6}}" /> // 086723
  1. Specifies the number of digits after the decimal point
<TextBox Text="{Binding Total, StringFormat={}{0:F4}}" /> // 28768234.9329
  1. Numbers separated by semicolons and specifying the number of digits after the decimal point
<TextBox Text="{Binding Total, StringFormat={}{0:N3}}" /> // 28,768,234.933
  1. formatted 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
  1. multiple binding
<TextBox.Text>
    <MultiBinding StringFormat="姓名:{0}{1}">
         <Binding Path="FristName" />
         <Binding Path="LastName" />
    </MultiBinding>
 </TextBox.Text>
// 姓名:AAbb
  1. Special characters in multiple bindings
<TextBox.Text>
     <MultiBinding StringFormat="姓名:{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.example.com

Keep Exploring

延伸阅读

更多文章
同分类 / 同标签 9/13/2025

Migration from WPF to Avalonia series: Why I have to migrate WPF programs to Avalonia

In the past few years, our host computer software has been mainly developed using WPF and WinForm. These technologies are really easy to use on the Windows platform, and they have also accompanied us through the stage of small-scale trial production to today's large-scale delivery. However, with the development of business and changes in customer needs, the single Windows technology stack has gradually become a hurdle that we must overcome.

继续阅读
同分类 / 同标签 1/26/2025

WPF internationalizes with custom XML files

This article describes in detail the methods of using custom XML files to achieve internationalization in WPF programs, including installing the necessary NuGet package, dynamically obtaining language lists, dynamically switching languages, using translation strings in code and xaml interfaces, etc. It also provides source code links to help developers easily internationalize WPF applications.

继续阅读