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
相关推荐
I'mSQL15 小时前
WPF资源字典合并报错
wpf
one99620 小时前
WPF应用程序中的异常处理
c#·.net·wpf
somethingGoWay2 天前
wpf .netcore 导出docx文件
wpf·.netcore
somethingGoWay2 天前
wpf .netcore 导出pdf文件
pdf·wpf·.netcore
self_myth3 天前
[特殊字符] 深入理解操作系统核心特性:从并发到分布式,从单核到多核的全面解析
windows·macos·wpf·harmonyos
c#上位机3 天前
wpf之TextBlock
c#·wpf
玉面小君4 天前
从 WPF 到 Avalonia 的迁移系列实战篇6:ControlTheme 和 Style区别
c#·wpf·avalonia
c#上位机5 天前
wpf之Border
c#·wpf
SunflowerCoder5 天前
WPF迁移avalonia之图像处理(一)
图像处理·wpf·avalonia
周杰伦fans5 天前
WPF中的DataContext以及常见的绑定方式
wpf