SoFunction
Updated on 2025-03-03

Sharing Binding string formatting skills in WPF

Preface

In WPF development, data binding is a core feature that allows UI elements to be closely associated with data sources. This article will describe how to use itStringFormatAttributes to format the bound data to make the data display more in line with the needs. With specific examples, you will learn how to easily control the presentation of bound data.

1. Currency format

<TextBlock Text="{Binding Price, StringFormat={}{0:C}}" /> // $123.46

2. Currency format, one decimal

<TextBox Text="{Binding Price, StringFormat={}{0:C1}}" /> // $123.5

3. Previous text

&lt;TextBox Text="{Binding Price, StringFormat=unit price:{0:C}}" /&gt; //Unit price: $123.46

4. Later text

&lt;TextBox Text="{Binding Price, StringFormat={}{0}Yuan}" /&gt; // RMB 123.45678

5. Fixed number of bits, the number of bits cannot be less than before formatting, only shaping is supported

<TextBox Text="{Binding Count, StringFormat={}{0:D6}}" /> // 086723

6. Specify the number of digits after the decimal point

<TextBox Text="{Binding Total, StringFormat={}{0:F4}}" /> // 28768234.9329

7. Numbers separated by semicolons and specify the number of digits after the decimal point

<TextBox Text="{Binding Total, StringFormat={}{0:N3}}" /> // 28,768,234.933

8. Format percentage

<TextBox Text="{Binding Persent, StringFormat={}{0:P1}}" /> // 78.9 %

9. Placeholder

<TextBox Text="{Binding Price, StringFormat={}{0:0000.00}}" /> // 0123.46
<TextBox Text="{Binding Price, StringFormat={}{0:####.##}}" /> // 123.46

10. Date/time

&lt;TextBox Text="{Binding DateTimeNow, StringFormat={}{0:d}}" /&gt; // 5/4/2015
&lt;TextBox Text="{Binding DateTimeNow, StringFormat={}{0:D}}" /&gt; // Monday, May 04, 2015
&lt;TextBox Text="{Binding DateTimeNow, StringFormat={}{0:f}}" /&gt; // Monday, May 04, 2015 5:46 PM
&lt;TextBox Text="{Binding DateTimeNow, StringFormat={}{0:F}}" /&gt; // Monday, May 04, 2015 5:46:56 PM
&lt;TextBox Text="{Binding DateTimeNow, StringFormat={}{0:g}}" /&gt; // 5/4/2015 5:46 PM
&lt;TextBox Text="{Binding DateTimeNow, StringFormat={}{0:G}}" /&gt; // 5/4/2015 5:46:56 PM
&lt;TextBox Text="{Binding DateTimeNow, StringFormat={}{0:m}}" /&gt; // May 04
&lt;TextBox Text="{Binding DateTimeNow, StringFormat={}{0:M}}" /&gt; // May 04
&lt;TextBox Text="{Binding DateTimeNow, StringFormat={}{0:t}}" /&gt; // 5:46 PM
&lt;TextBox Text="{Binding DateTimeNow, StringFormat={}{0:T}}" /&gt; // 5:46:56 PM
&lt;TextBox Text="{Binding DateTimeNow, StringFormat={}{0:yyyyYearMMmoonddday}}" /&gt; // May 4, 2015&lt;TextBox Text="{Binding DateTimeNow, StringFormat={}{0:yyyy-MM-dd}}" /&gt; // 2015-05-04
&lt;TextBox Text="{Binding DateTimeNow, StringFormat={}{0:yyyy-MM-dd HH:mm}}" /&gt; // 2015-05-04 17:46
&lt;TextBox Text="{Binding DateTimeNow, StringFormat={}{0:yyyy-MM-dd HH:mm:ss}}" /&gt; // 2015-05-04 17:46:56

or

<TextBlock Text="{Binding Time,StringFormat='yyyy:MM:dd HH:mm:ss'}"/>

11. Multiple bindings

&lt;&gt;
    &lt;MultiBinding StringFormat="Name:{0}{1}"&gt;
        &lt;Binding Path="FristName" /&gt;
        &lt;Binding Path="LastName" /&gt;
    &lt;/MultiBinding&gt;
&lt;/&gt;
// Name: AAbb

12. Special characters in multiple bindings

&lt;&gt;
    &lt;MultiBinding StringFormat="Name:{0}&amp;#x09;{1}"&gt;
        &lt;Binding Path="FristName" /&gt;
        &lt;Binding Path="LastName" /&gt;
    &lt;/MultiBinding&gt;
&lt;/&gt;
 &lt;!--
 \a  &amp;#x07;  BEL
 \b  &amp;#x08;  BS - Backspace
 \f  &amp;#x0c;  FF - Formfeed
 \n  &amp;#x0a;  LF, NL - Linefeed, New Line
 \r  &amp;#x0d;  CR - Carriage return
 \t  &amp;#x09;  HT - Tab, Horizontal Tabelator
 \v  &amp;#x0b;  VT - Vertical Tabelator 
 --&gt;
// Name: AA bb

Summarize

This article demonstrates how to utilize it in WPF through examplesStringFormatFormat the various types of bound data. Favorite these methods not only improve the flexibility of data presentation, but also enhance the usability of the application. After mastering this technique, you can manage and display data in the application more effectively.

This is the article about the sharing of Binding string formatting techniques in WPF. For more related WPF Binding string formatting content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!