WPF StringFormat 格式化文本
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>
常见的特殊字符:
小于号(<) <
大于号(>) >
&符号(&) &
引号(") "
单引号(') '
回车
换行
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 格式化文本的更多相关文章
- WPF 语言格式化文本控件
前言 本章讲述正确添加语言资源的方式,以及一段语言资源的多种样式显示. 例如:“@Winter,你好!感谢已使用软件 800 天!” 在添加如上多语言资源项时,“XX,你好!感谢已使用软件 X 天!” ...
- WPF中StringFormat 格式化 的用法
原文 WPF中StringFormat 格式化 的用法 网格用法 <my:DataGridTextColumn x:Name="PerformedDate" Header=& ...
- WPF中Binding使用StringFormat格式化字符串方法
原文:WPF中Binding使用StringFormat格式化字符串方法 货币格式 <TextBlock Text="{Binding Price, StringFormat={}{0 ...
- 解决方案:带格式化文本控件( RichText)的模板如果在InfoPath的浏览器中加载可能出现 COM 组件的80040154错误
建议大家在微软的组件出现问题时,在GOOGLE上搜索解决方案,一般来说,总有结果: 带格式化文本控件( RichText)的模板如果在InfoPath的浏览器中加载,可能出现 COM 组件的80 ...
- c# 正则格式化文本防止SQL注入
/// <summary> /// 格式化文本(防止SQL注入) /// </summary> /// <param name="str">&l ...
- python中使用%与.format格式化文本
初学python,看来零零碎碎的格式化文本的方法,总结一下python中格式化文本的方法.使用不当的地欢迎指出谢谢. 1.首先看使用%格式化文本 常见的占位符: 常见的占位符有: %d 整数 %f 浮 ...
- 格式化文本数据抽取工具awk
在管理和维护Linux系统过程中,有时可能需要从一个具有一定格式的文本(格式化文本)中抽取数据,这时可以使用awk编辑器来完成这项任务.发明这个工具的作者是Aho.Weinberg和Kernighan ...
- WPF中TextBlock文本换行与行间距
原文:WPF中TextBlock文本换行与行间距 换行符: C#代码中:\r\n 或 \r 或 \n XAML中: 或 注:\r 回车 (carriage return 缩写),\n 新行 (new ...
- WPF中的文本度量
关于WPF中的文本度量,需要了解以下几个问题: WPF中支持一些常用的度量单位:px(device independent pixels).in(inches).cm(centimeters).pt( ...
随机推荐
- 使用kolin开发你的android应用
转载请注明出处,谢谢! 前段时间花了大概三周时间学习了kotlin,借着kotlin正好发布1.2,使用kotlin撸了一个android demo Github地址:https://github.c ...
- PLC不能初始化问题
检索COM 类工厂中 CLSID 为 <28e68f9a-8d75-11d1-8dc3-3c302a000000> 的组件时失败,原因是出现以下错误: 80040154 解决方案: Win ...
- web移动端开发技巧
一.meta的使用 1.<meta name="viewport" content="width=device-width,initial-scale=1.0, m ...
- http://www.layui.com/doc/modules/laydate.html实时通信\日期、==插件
8520**ali chengyouli http://www.layui.com/doc/modules/laydate.html实时通信\日期.==插件
- Tomcat 集群中 实现session 共享的三种方法
前两种均需要使用 memcached 或 redis 存储 session ,最后一种使用 terracotta 服务器共享. 建议使用 redis ,不仅仅因为它可以将缓存的内容持久化,还因为它支持 ...
- FFmpeg 结构体学习(二): AVStream 分析
在上文FFmpeg 结构体学习(一): AVFormatContext 分析我们学习了AVFormatContext结构体的相关内容.本文,我们将讲述一下AVStream. AVStream是存储每一 ...
- [Swift]LeetCode80. 删除排序数组中的重复项 II | Remove Duplicates from Sorted Array II
Given a sorted array nums, remove the duplicates in-place such that duplicates appeared at most twic ...
- [Swift]LeetCode135. 分发糖果 | Candy
There are N children standing in a line. Each child is assigned a rating value. You are giving candi ...
- [Swift]LeetCode611. 有效三角形的个数 | Valid Triangle Number
Given an array consists of non-negative integers, your task is to count the number of triplets chose ...
- [Swift]LeetCode782. 变为棋盘 | Transform to Chessboard
An N x N board contains only 0s and 1s. In each move, you can swap any 2 rows with each other, or an ...