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
相关推荐
samble1 天前
从零搭建半导体设备上位机(十二):参数设置页,表单校验、范围校验与配方联动
wpf·数据校验·validationrule
samble10 天前
从零搭建半导体设备上位机(九):拖拽与交互,Adorner、DragDrop 与晶圆拖放信息
wpf·拖拽·adorner
郝学胜-神的一滴11 天前
AI 编程智能体 27:吃透MCP通信机制
人工智能·python·langchain·wpf·numpy·scikit-learn
波力海苔夹心脆67512 天前
C# WPF 资源字典详解:ResourceDictionary、MergedDictionaries 与自定义控件案例
开发语言·c#·wpf·入门教程·resource·资源字典
陈仓主教12 天前
# WPF CheckBox 控件详解与使用—WPF 中的控件(6)--复选框控件(CheckBox)
wpf
光泽雨14 天前
WPF 音视频四大核心方案对比
wpf·音视频
浪扼飞舟15 天前
WPF(数据绑定)
wpf
samble16 天前
从零搭建半导体设备上位机(四):单实例、启动流程与配置文件,App 里那 526 行到底在干什么
wpf·配置文件·单实例
浪扼飞舟16 天前
WPF(控件的继承树、内容控件、布局控件)
wpf
avi911119 天前
电影开源Jevdo - 源码介绍(vue版)没有android 和 没有wpf
android·wpf·框架·数据爬虫·meego·电影管理