StringFormat对特定数据格式的转换

WPF中,对数字/日期等的格式化,可参考此篇博客:https://www.cnblogs.com/zhengwen/archive/2010/06/19/1761036.html

StringFormat对语言项的格式化

1.单个动态数据绑定

例如:

“已使用此软件 365 天!”,WPF中可如下处理

添加资源项:

<system:String x:Key="LangSource1">已使用此软件 {0} 天!</system:String>

StringFormat格式化:

<TextBlock Text="{Binding UsedDays,StringFormat={StaticResource LangSource1}}"/>

文本:《365》

StringFormat格式化:

  1. <TextBlock Text="{Binding UsedDays,StringFormat=《{0}》}" Foreground="#018000"/>

其它案例:

  1. <TextBox Text="{Binding Value, StringFormat={}{0:0000.0}}" /> // 0123.4
  2. <TextBox Text="{Binding Value, StringFormat={}{0:####.#}}" /> // 123.4

2.多个动态数据绑定

例如:“30/365”

  1. <TextBlock>
  2. <TextBlock.Text>
  3. <MultiBinding StringFormat="{}{0}/{1}">
  4. <Binding Path="LearnedDays" FallbackValue="0" />
  5. <Binding Path="PlanningDays" FallbackValue="0" />
  6. </MultiBinding>
  7. </TextBlock.Text>
  8. </TextBlock>

例如:“已经学习30天,剩余计划学习天数365”

添加资源项:

<system:String x:Key="LangSource5">已经学习{0},剩余计划学习天数{1}</system:String>

WPF中stringFormat处理:

  1. <TextBlock>
  2. <TextBlock.Text>
  3. <MultiBinding StringFormat="{StaticResource LangSource5}">
  4. <Binding Path="LearnedDays" FallbackValue="0" />
  5. <Binding Path="PlanningDays" FallbackValue="0" />
  6. </MultiBinding>
  7. </TextBlock.Text>
  8. </TextBlock>
  1. <TextBox.Text>
  2. <MultiBinding StringFormat="姓名:{0} {1}">
  3. <Binding Path="FristName" />
  4. <Binding Path="LastName" />
  5. </MultiBinding>
  6. </TextBox.Text>

常见的特殊字符:

小于号(<) &lt;
大于号(>) &gt;
&符号(&) &amp;
引号(") &quot;
单引号(') &apos;
回车
换行
Tab
空格

3. 数据格式转换与动态数据绑定的灵动结合

例如:“学生张三的期末平均成绩为93.20分”

添加资源项:

<system:String x:Key="LangSource5">学生{0}的期末平均成绩为{1:N2}分</system:String>

WPF中stringFormat处理:

  1. <TextBlock>
  2. <TextBlock.Text>
  3. <MultiBinding StringFormat="{StaticResource LangSource5}">
  4. <Binding Path="Name"/>
  5. <Binding Path="Score"/>
  6. </MultiBinding>
  7. </TextBlock.Text>
  8. </TextBlock>

WPF StringFormat 格式化文本的更多相关文章

  1. WPF 语言格式化文本控件

    前言 本章讲述正确添加语言资源的方式,以及一段语言资源的多种样式显示. 例如:“@Winter,你好!感谢已使用软件 800 天!” 在添加如上多语言资源项时,“XX,你好!感谢已使用软件 X 天!” ...

  2. WPF中StringFormat 格式化 的用法

    原文 WPF中StringFormat 格式化 的用法 网格用法 <my:DataGridTextColumn x:Name="PerformedDate" Header=& ...

  3. WPF中Binding使用StringFormat格式化字符串方法

    原文:WPF中Binding使用StringFormat格式化字符串方法 货币格式 <TextBlock Text="{Binding Price, StringFormat={}{0 ...

  4. 解决方案:带格式化文本控件( RichText)的模板如果在InfoPath的浏览器中加载可能出现 COM 组件的80040154错误

      建议大家在微软的组件出现问题时,在GOOGLE上搜索解决方案,一般来说,总有结果:  带格式化文本控件( RichText)的模板如果在InfoPath的浏览器中加载,可能出现 COM 组件的80 ...

  5. c# 正则格式化文本防止SQL注入

    /// <summary> /// 格式化文本(防止SQL注入) /// </summary> /// <param name="str">&l ...

  6. python中使用%与.format格式化文本

    初学python,看来零零碎碎的格式化文本的方法,总结一下python中格式化文本的方法.使用不当的地欢迎指出谢谢. 1.首先看使用%格式化文本 常见的占位符: 常见的占位符有: %d 整数 %f 浮 ...

  7. 格式化文本数据抽取工具awk

    在管理和维护Linux系统过程中,有时可能需要从一个具有一定格式的文本(格式化文本)中抽取数据,这时可以使用awk编辑器来完成这项任务.发明这个工具的作者是Aho.Weinberg和Kernighan ...

  8. WPF中TextBlock文本换行与行间距

    原文:WPF中TextBlock文本换行与行间距 换行符: C#代码中:\r\n 或  \r 或 \n XAML中: 或 注:\r 回车 (carriage return 缩写),\n 新行 (new ...

  9. WPF中的文本度量

    关于WPF中的文本度量,需要了解以下几个问题: WPF中支持一些常用的度量单位:px(device independent pixels).in(inches).cm(centimeters).pt( ...

随机推荐

  1. [error] - Build path is incomplete. Cannot find class file for org/aspectj/weaver/refl

    将本地仓库中mybatis 的jar 包删除,然后在eclipse 中右键工程选中 Maven->upgrade ..

  2. d3.js做的柱状图

    window.onload = function(){ var dataArray = [23, 13, 21, 14, 37, 15, 18, 34, 30]; var height = 400,w ...

  3. CRT破解版

    1.先去https://www.ttrar.com/html/VanDyke-SecureCRT.html上面下载一个CRT软件 2.下载一个注册机 http://www.ddooo.com/soft ...

  4. Oracle--配置并保存PL/SQL Developer界面

    之前一直用SQL Server,现在刚接触Oracle,用PL/SQL Developer 客户端,在设置自已的使用习惯后保存界面 PL/SQL Developer初始界面布局,当你设置后,重新启动, ...

  5. Swift 对象内存模型探究(一)

    本文来自于腾讯Bugly公众号(weixinBugly),未经作者同意,请勿转载,原文地址:https://mp.weixin.qq.com/s/zIkB9KnAt1YPWGOOwyqY3Q 作者:王 ...

  6. Angularjs中的缓存以及缓存清理

    写在最前面:这篇博文是2篇文章组成,详细介绍了Angularjs中的缓存以及缓存清理,文章由上海尚学堂转载过来,欢迎大家阅读和评论.转载请注明出处,谢谢! 一个缓存就是一个组件,它可以透明地储存数据, ...

  7. MySQL 数据库字段类型使用说明

    简介 MySQL支持大量的列类型,它可以被分为3类:数字类型.日期和时间类型以及字符串(字符)类型. 数值类型 下列用于描述的代码字母中: M表示最大的显示尺寸.最大的合法的显示尺寸是 255 .(注 ...

  8. [Bash]LeetCode194. 转置文件 | Transpose File

    Given a text file file.txt, transpose its content. You may assume that each row has the same number ...

  9. [Swift]LeetCode372. 超级次方 | Super Pow

    Your task is to calculate ab mod 1337 where a is a positive integer and bis an extremely large posit ...

  10. [Swift]LeetCode482. 密钥格式化 | License Key Formatting

    You are given a license key represented as a string S which consists only alphanumeric character and ...