WPF XAML之bing使用StringFormat(转)
释义
BindingBase.StringFormat 属性
获取或设置一个字符串,该字符串指定如果绑定值显示为字符串,应如何设置该绑定的格式。
命名空间: System.Windows.Data
程序集: PresentationFramework(在 PresentationFramework.dll 中)
用于 XAML 的 XMLNS:http://schemas.microsoft.com/winfx/2006/xaml/presentation,http://schemas.microsoft.com/netfx/2007/xaml/presentation
StringFormat和Converter
如果设置 Converter 和 StringFormat 属性,则会先对数据值应用转换器,然后应用 StringFormat。
使用
1,Binding中使用StringFormat, StringFormat 设置为撰写字符串格式时,只能指定一个参数。如绑定Name:
<ListView ItemsSource="{StaticResource MyData}">
<ListView.View>
<GridView>
<GridViewColumn DisplayMemberBinding="{Binding Path=Description}"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=Price, StringFormat=Now {0:c}!}"/>
<GridViewColumn DisplayMemberBinding="{Binding Path=Price, StringFormat={}{0:c}!}"/>
</GridView>
</ListView.View>
</ListView>
注意:
如果StringFormat中没有字符,“StringFormat=”后面需要先加入“{}”。
如果StringFormat中有字符,则不需要加入“{}”
2,绑定格式化时间
<TextBlock Text="{Binding Date, StringFormat={}{0:MM/dd/yyyy}}" />
或者
<TextBlock Text="{Binding Time,StringFormat='yyyy:MM:dd HH:mm:ss'}"/>
3,多重绑定
<ListBox ItemsSource="{StaticResource MyData}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}{0} -- Now only {1:C}!">
<Binding Path="Description"/>
<Binding Path="Price"/>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
4,多重绑定中的特殊字符, 如 \t
<TextBlock.Text>
<MultiBinding StringFormat="Delete {0} {1}">
<Binding Path="FirstName" />
<Binding Path="LastName" />
</MultiBinding>
</TextBlock.Text>
特殊字符如下:
- \a  BEL
- \b  BS - Backspace
- \f  FF - Formfeed
- \n LF, NL - Linefeed, New Line
- \r CR - Carriage return
- \t HT - Tab, Horizontal Tabelator
- \v  VT - Vertical Tabelator
5,在使用 PriorityBinding 时,可以在 PriorityBinding 和/或子绑定对象上设置 StringFormat。
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"
DataContext="{Binding Source={StaticResource AsyncDS}}">
<TextBlock FontSize="18" FontWeight="Bold" Margin="10" HorizontalAlignment="Center">Priority Binding</TextBlock>
<TextBlock Background="Honeydew" Width="100" HorizontalAlignment="Center">
<TextBlock.Text>
<PriorityBinding FallbackValue="defaultvalue">
<Binding Path="SlowestDP" IsAsync="True"/>
<Binding Path="SlowerDP" IsAsync="True"/>
<Binding Path="FastDP" />
</PriorityBinding>
</TextBlock.Text>
</TextBlock>
</StackPanel>
关于此条更多信息看:http://msdn.microsoft.com/zh-cn/library/vstudio/system.windows.data.prioritybinding.aspx
WPF XAML之bing使用StringFormat(转)的更多相关文章
- WPF XAML之bing使用StringFormat
WPF XAML之bing使用StringFormat // 转化为百分比 Text="{Binding Progress, StringFormat=\{0:P\}}" < ...
- [WPF,XAML] 跳动的心
原文:[WPF,XAML] 跳动的心 没什么艺术细胞,原谅,原谅! <Canvas Width="0" Height="0"> <Canvas ...
- WPF XAML
xmlns 在xml中专门用于声明名字控件, xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 是 ...
- 使用MVVM DataTemplate在WPF XAML视图之间切换
原文 使用MVVM DataTemplate在WPF XAML视图之间切换 更新:这个技术的改进版本,一个不创建视图,可以在以下链接找到: http://www.technical-recipes.c ...
- 使用MVVM DataTriggers在WPF XAML视图之间切换/Window窗口自适应内容大小并居中
原文 使用MVVM DataTriggers在WPF XAML视图之间切换 相关文章: http://www.technical-recipes.com/2016/switching-between- ...
- 标记扩展和 WPF XAML
本主题介绍 XAML 的标记扩展概念,包括其语法规则.用途以及底层的类对象模型. 标记扩展是 XAML 语言以及 XAML 服务的 .NET 实现的常规功能. 本主题专门详细论述了用于 WPF X ...
- WPF在XAML中Binding使用StringFormat属性
1. 绑定Currency, 如果没有字符的话, =后面需要先加入{}. 不加的话会出问题. 1 <TextBlock Text="{Binding Amount, StringFor ...
- WPF - XAML如何引入名字空间
WPF 的XAML引入名字空间的概念,经常容易让人混淆.如何引入名字空间,并且在XAML中调用其中的类,下面给一个简单的介绍. 比如我们有一个Hepler类. namespace Wheat.PIMS ...
- WPF XAML 资源样式模板属性存放位置
WPF的XAML 资源申明 类似HTML. 整体来说分3种1.行类资源样式属性 1.1 行内属性 <Button Content="按钮" Foreground=" ...
随机推荐
- HTML5 Canvas显示本地图片实例1、Canvas预览图片实例1
1.前台代码: <input id="fileOne" type="file" /> <canvas id="canvasOne&q ...
- Html.Action和Html.RederAction来创建子视图
1. 父视图和子视图 父视图是包含了调用返回子视图的动作方法的视图. 父视图包含大部分用于呈现页面的HTML.子视图仅包含用于展示视图某部分的必须的标记. 例如,一个子视图创建一个列表,视图可能仅仅包 ...
- 附加数据库报错:无法打开物理文件 XXX.mdf",操作系统错误 5:"5(拒绝访问。)"
今天在附加数据库的时候出现如图报错信息: 无法打开物理文件 XXX.mdf",操作系统错误 5:"5(拒绝访问.)"错信息如图:(是不是远程服务器数据库附加出现只读那个情 ...
- 关于textView的字数限制
在一个项目中遇到texteView的字数限制,在iOS7.0上,会出现崩溃.我在这里栽了一个大跟头,废话不多说,下面直接贴代码吧. - (void)textViewDidChange:(UITextV ...
- 使用 Nginx 来反向代理多个 NoderCMS
Nginx ("engine x") 是一个高性能的HTTP和反向代理服务器,也是一个IMAP/POP3/SMTP服务器.Nginx是由Igor Sysoev为俄罗斯访问量第二的R ...
- cmd编译运行Java文件详解
①准备工作 首先用记事本编写HelloWorld.java放至G:\Javaspace路径 public class HelloWorld{ public static void main(Strin ...
- Java系列--第五篇 基于Maven的SSME之Token及Parameterized单元测试
本来在第四篇要说完的,但是写着写着,我觉得内容有点多起来了,所以就另开这篇,在这里专门讲述Token的定义,JSP自定义标签以及如何用Parameterized的来做单元测试. 1,新建包com.va ...
- PHP 中const 与define 区别
1.const用于类成员变量定义,一旦定义且不能改变其值.define定义全局常量,在任何地方都可以访问. 2.define不能在类中定义而const可以. 3.const不能在条件语句中定义常量 i ...
- SDWebImage 官方文档
API documentation is available at CocoaDocs - SDWebImage Using UIImageView+WebCache category with UI ...
- 【杭州图铭科技有限公司招募贴】——“JUST DO IT”
I'm convinced that the only thing that kept me going was that I loved what I did. ——Steve Paul Jobs( ...