wpf binding中的 stringformat属性

csharp 复制代码
<StackPanel>
    <TextBlock Text="{Binding Info,
    StringFormat={}{0:C}}"/>
    <TextBlock Text="{Binding Info,
    StringFormat=Number: {0}}"/>
    <TextBlock Text="{Binding Info,
    StringFormat=Seconds: {0} (s)}"/>
    <TextBlock Text="{Binding Info,
    StringFormat={}{0:F2} km}"/>
    <TextBlock Text="{Binding Number,
    StringFormat={}{0:N2} km}"/>
    <TextBlock Text="{Binding Info,
    StringFormat={}{0:G4} km}"/>
    <TextBlock Text="{Binding Info,
    StringFormat={}{0:E4} km}"/>
    <TextBlock Text="{Binding Info,
    StringFormat={}{0:P3} Percentage}"/>
</StackPanel>

为了使用 {0:x} 类似的格式化模式,必须保证有前缀或者 {} 表示空前缀,

例如第一个 {}{0:C} 表示了一个空前缀按照货币显示格式化数字,默认是2位小数,{}{0:Cn},n 表示保留的小数位数

第二个 Number: {0} 使用 Number: 作为前缀 {0} 则代表格式化的属性。

下面便是一些 ToString() 中常用的格式化标识,包括 F2 保留2位小数,N2 保留2位小数并使用逗号分隔千分位,G4 保留4个有效数字,E4 使用科学计数法并保留4位小数位,P3 使用百分数表示法并保留3位小数。

》》》占位符

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

》》》日期、时间

csharp 复制代码
<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'}"/> // 2015:05:04 17:46:56

》》多重绑定

csharp 复制代码
<TextBox.Text>
	<MultiBinding StringFormat="姓名:{0}{1}">
	<Binding Path="FristName" />
	<Binding Path="LastName" />
	</MultiBinding>
</TextBox.Text>
// 姓名:AAbb
相关推荐
heimeiyingwang12 小时前
【架构实战】日志体系ELK:集中化日志管理实践
elk·架构·wpf
CPU不够了13 小时前
WPF 多选下拉+搜索过滤_wpf下拉选项增加搜索
wpf
FuckPatience13 小时前
WPF 列表控件自动拉伸子元素的宽度
wpf
LCG元13 小时前
【Go后端开发】从 0 到生产级:高性能分布式网关全实现 + 接口限流熔断降级实战
分布式·golang·wpf
枫叶林FYL1 天前
项目九:异步高性能爬虫与数据采集中枢 —— 基于 Crawl<sub>4</sub>AI 与 Playwright 的现代化数据采集平台 项目总览
爬虫·python·深度学习·wpf
她说彩礼65万1 天前
WPF 多值转换器
wpf
无心水2 天前
【分布式利器:金融级】金融级分布式架构开源框架全景解读
人工智能·分布式·金融·架构·开源·wpf·金融级框架
她说彩礼65万2 天前
WPF 转换器
wpf
WPF工业上位机2 天前
匠心研智造,同心赴新程-WPF硬件通讯之串口&Socket
wpf
爱炸薯条的小朋友2 天前
C#由窗体原子表溢出造成的软件闪退,根本原因补充
开发语言·c#·wpf