WPF中XAML中使用String.Format格式化字串範例

WPF中XAML中使用String.Format格式化字串範例

字串格式化

最後更新 2022/5/22 下午9:50
眾尋
預計閱讀 3 分鐘
分類
WPF
標籤
.NET WPF
  1. 貨幣格式
<TextBlock Text="{Binding Price, StringFormat={}{0:C}}" /> // $123.46
  1. 貨幣格式,一位小數
<TextBox Text="{Binding Price, StringFormat={}{0:C1}}" /> // $123.5
  1. 前置文字
<TextBox Text="{Binding Price, StringFormat=單價:{0:C}}" /> //單價:$123.46
  1. 後置文字
<TextBox Text="{Binding Price, StringFormat={}{0}元}" /> // 123.45678元
  1. 固定位數,位數不能少於未格式化前,僅支援整數
<TextBox Text="{Binding Count, StringFormat={}{0:D6}}" /> // 086723
  1. 指定小數點後的位數
<TextBox Text="{Binding Total, StringFormat={}{0:F4}}" /> // 28768234.9329
  1. 用逗號分隔的數字,並指定小數點後的位數
<TextBox Text="{Binding Total, StringFormat={}{0:N3}}" /> // 28,768,234.933
  1. 格式化百分比
<TextBox Text="{Binding Persent, StringFormat={}{0:P1}}" /> // 78.9 %
  1. 預留位置符號
<TextBox Text="{Binding Price, StringFormat={}{0:0000.00}}" /> // 0123.46
<TextBox Text="{Binding Price, StringFormat={}{0:####.##}}" /> // 123.46
  1. 日期/時間
<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. 多重繫結
<TextBox.Text>
    <MultiBinding StringFormat="姓名:{0}{1}">
         <Binding Path="FristName" />
         <Binding Path="LastName" />
    </MultiBinding>
 </TextBox.Text>
// 姓名:AAbb
  1. 多重繫結中的特殊字元
<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
-->

原文作者:眾尋

原文連結:https://www.cnblogs.com/ZXdeveloper/p/15513657.html

繼續探索

延伸閱讀

更多文章
同分類 / 同標籤 2025/1/26

WPF 藉助自訂 XML 檔案實現國際化

本文詳細介紹了在WPF程式中使用自訂XML檔案實現國際化的方法,包括安裝必備NuGet套件、動態獲取語言清單、動態切換語言、在程式碼和XAML介面中使用翻譯字串等內容,同時提供了原始碼連結,幫助開發者輕鬆實現WPF應用程式的國際化。

繼續閱讀