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格式化:

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

其它案例:

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

2.多个动态数据绑定

例如:“30/365”

     <TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0}/{1}">
<Binding Path="LearnedDays" FallbackValue="0" />
<Binding Path="PlanningDays" FallbackValue="0" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>

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

添加资源项:

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

WPF中stringFormat处理:

     <TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{StaticResource LangSource5}">
<Binding Path="LearnedDays" FallbackValue="0" />
<Binding Path="PlanningDays" FallbackValue="0" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
 <TextBox.Text>
<MultiBinding StringFormat="姓名:{0} {1}">
<Binding Path="FristName" />
<Binding Path="LastName" />
</MultiBinding>
</TextBox.Text>

常见的特殊字符:

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

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

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

添加资源项:

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

WPF中stringFormat处理:

     <TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{StaticResource LangSource5}">
<Binding Path="Name"/>
<Binding Path="Score"/>
</MultiBinding>
</TextBlock.Text>
</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. LSI IBM服务器阵列卡操作经历

    说明:因为服务器的一个磁盘坏了,因为没有经验不敢操作.正好有一台撤下来的相同服务器,所以查找了各种教程,研究了一下各种操作.记录在这里,防止忘记.一.概念说明raid(自己百度)阵列卡组(group) ...

  2. 替换url中的域名

    /**     *      * @param domain 域名     * @param port   端口号     * @param url    url路径     * @return   ...

  3. java中List<Map<String, Object>>关于null的判断

    List<Map<String, Object>> selectTmFileInfo = fileInfoService.selectTmFileInfoByToken(cTo ...

  4. react-native-upgrade-android

    React Native的版本升级插件(仅是android), react-native版本需要0.17.0及以上 如何安装 1.首先安装npm包 npm install react-native-u ...

  5. Anveshak: Placing Edge Servers In The Wild

    Anveshak:在野外放置边缘服务器 本文为SIGCOMM 2018 Workshop (Mobile Edge Communications, MECOMM)论文. 笔者翻译了该论文.由于时间仓促 ...

  6. 巧妙设置Texture Type,将ShadowMask内存占用变成之前的1/4

    0x00 前言 在这篇文章中,我选择了过去一周Unity官方社区交流群中比较有代表性的几个问题,总结在这里和大家进行分享.同时,也欢迎大家加入我们这个讨论干货的官方技术群,交流看法分享经验. Unit ...

  7. javascript 省市区三级联动 附: json数据

    html: <label> <span>购买地址</span> <select name="PurchaseProvince" style ...

  8. [Swift]LeetCode419. 甲板上的战舰 | Battleships in a Board

    Given an 2D board, count how many battleships are in it. The battleships are represented with 'X's, ...

  9. [Swift]LeetCode530. 二叉搜索树的最小绝对差 | Minimum Absolute Difference in BST

    Given a binary search tree with non-negative values, find the minimum absolute difference between va ...

  10. [Swift]LeetCode553. 最优除法 | Optimal Division

    Given a list of positive integers, the adjacent integers will perform the float division. For exampl ...