WPF中Binding字符串格式化技巧分享
前言
在 WPF 開發(fā)中,數(shù)據(jù)綁定(Binding)是一項核心功能,它允許 UI 元素與數(shù)據(jù)源緊密關(guān)聯(lián)。本文將介紹如何使用 StringFormat 屬性來格式化綁定的數(shù)據(jù),使數(shù)據(jù)顯示更加符合需求。通過具體的例子,您將學(xué)會如何輕松地控制綁定數(shù)據(jù)的呈現(xiàn)形式。
1、貨幣格式
<TextBlock Text="{Binding Price, StringFormat={}{0:C}}" /> // $123.46
2、貨幣格式,一位小數(shù)
<TextBox Text="{Binding Price, StringFormat={}{0:C1}}" /> // $123.5
3、前文字
<TextBox Text="{Binding Price, StringFormat=單價:{0:C}}" /> //單價:$123.46
4、后文字
<TextBox Text="{Binding Price, StringFormat={}{0}元}" /> // 123.45678元
5、固定的位數(shù),位數(shù)不能少于未格式化前,僅支持整形
<TextBox Text="{Binding Count, StringFormat={}{0:D6}}" /> // 086723
6、指定小數(shù)點后的位數(shù)
<TextBox Text="{Binding Total, StringFormat={}{0:F4}}" /> // 28768234.9329
7、用分號隔開的數(shù)字,并指定小數(shù)點后的位數(shù)
<TextBox Text="{Binding Total, StringFormat={}{0:N3}}" /> // 28,768,234.933
8、格式化百分比
<TextBox Text="{Binding Persent, StringFormat={}{0:P1}}" /> // 78.9 %
9、占位符
<TextBox Text="{Binding Price, StringFormat={}{0:0000.00}}" /> // 0123.46
<TextBox Text="{Binding Price, StringFormat={}{0:####.##}}" /> // 123.46
10、日期/時間
<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
或者
<TextBlock Text="{Binding Time,StringFormat='yyyy:MM:dd HH:mm:ss'}"/>
11、多重綁定
<TextBox.Text>
<MultiBinding StringFormat="姓名:{0}{1}">
<Binding Path="FristName" />
<Binding Path="LastName" />
</MultiBinding>
</TextBox.Text>
// 姓名:AAbb
12、多重綁定中的特殊字符
<TextBox.Text>
<MultiBinding StringFormat="姓名:{0}	{1}">
<Binding Path="FristName" />
<Binding Path="LastName" />
</MultiBinding>
</TextBox.Text>
<!--
\a  BEL
\b  BS - Backspace
\f  FF - Formfeed
\n 
 LF, NL - Linefeed, New Line
\r 
 CR - Carriage return
\t 	 HT - Tab, Horizontal Tabelator
\v  VT - Vertical Tabelator
-->
// 姓名:AA bb
總結(jié)
本文通過實例演示了如何在 WPF 中利用 StringFormat 對綁定的各類數(shù)據(jù)進(jìn)行格式化處理。收藏這些方法不僅提高了數(shù)據(jù)展示的靈活性,還增強(qiáng)了應(yīng)用程序的可用性。掌握這一技巧后,大家可以更有效地管理和顯示應(yīng)用程序中的數(shù)據(jù)。
到此這篇關(guān)于WPF中Binding字符串格式化技巧分享的文章就介紹到這了,更多相關(guān)WPF Binding字符串格式化內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
CefSharp如何進(jìn)行頁面的縮放(Ctrl+滾輪)
CefSharp簡單來說就是一款.Net編寫的瀏覽器包,本文主要介紹了CefSharp如何進(jìn)行頁面的縮放,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06
使用aspose.word 第三方的插件實現(xiàn)導(dǎo)出word
本文給大家分享的是一個使用使用aspose.word 第三方的插件實現(xiàn)導(dǎo)出word的實例,十分的實用,有需要的小伙伴可以參考下。2015-06-06
C#并行編程之?dāng)?shù)據(jù)并行Tasks.Parallel類
這篇文章介紹了C#并行編程之?dāng)?shù)據(jù)并行Tasks.Parallel類,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05
UnityWebRequest前后端交互實現(xiàn)過程解析
這篇文章主要介紹了UnityWebRequest前后端交互實現(xiàn)過程解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-06-06

