Silverlight 4 Binding and StringFormat in XAML
来源:互联网 发布:java sql解析器 开源 编辑:程序博客网 时间:2024/05/18 04:35
I discovered this amazing feature almost by accident and it has made this one part of my design so much easier that I had to share it.
A new feature in Silverlight 4 is the ability to using the StringFormat feature when binding. Previously, if I wanted to have a piece of text that said “Your name is [username]” I could either use the old Horizontal-Stack-Panel-And-2-TextBlocks trick (as seen below)…
<StackPanel Orientation=”Horizontal”>
<TextBlock Text=”Your name is” Margin=”0,0,4,0″/>
<TextBlock Text=”{Binding username}“/>
</StackPanel>
…or write a value converter (not going to be seen below because there’s a great example of it over here. Incidentally, that example is totally irrelevant if you’re going to use StringFormat, but more on that in a second).
The StringFormat option in Silverlight 4 allows you put all that information into a single field, which is extremely useful not only for TextBlocks, but for Content fields in a Button. In fact, let’s use that as an example.
Let’s say you want to create a Button to log out, so you want it to say “Log Out of <Username> Account”. (A bit clumsy, but the technique is the important part.) All you would have to do is the following:
<Button Content=”{Binding username, StringFormat=’Log Out of {0} Account’}“/>
This gets even better for things like number formatting. Let’s say we want the user to enter an amount of money (for example, $1,593.29) into a TextBox (maybe in a PayPal application). If we have bound that value to a numeric format, we can express that format through binding and when the TextBox loses focus, the StringFormat will take the number entered and format in a currency format.
<TextBox Text=”{Binding paymentAmount, StringFormat={0:C2}}“/>
The only issue with numerical and date formats is they the MUST be bound to a number or date.
With that in mind, here is a sampling of StringFormat options, stolen mostly from Kathy Kam. For more complete options, check out the MSDN articles on String.Format and trial-and-error your way through things. If you want to play around with this, download my StringFormat project or look at the Silverlight sample app at the bottom of this page.
Strings
For a string with the value “Silverlight”
Using {0,#} effectively forces the string to be at least # characters long, using spaces to pad it to the requested length.
StringFormat={0,20} : “ Silverlight”
StringFormat={0,-20} : “Silverlight ”
StringFormat=’I just typed "{0}".’ : “I just typed “Silverlight”.”
MSDN article on Composite Formatting
Numbers
For a double with the value : “38293.53”
StringFormat=c : “$38,293.53” – Use ‘c’ for currency
StringFormat=e : “3.829353e+004” – Use ‘e’ for exponential (scientific)
StringFormat=n : “38,293.53” – Use n for number
You can also use these in the following format:
{0:(letter)(number)}
where (number) indicates how many decimal places there should be. The format will use standard rounding rules to determine the last digit. For example:
StringFormat={0:c0} : “$38,294”
StringFormat={0:n4} : “38,293.5300”
StringFormat=You have {0:c1} : “You have $38,293.5”
MSDN article for standard number formatting
MSDN article for custom number formatting
Dates
The date formatting has a huge range of options.
For the DateTime of “April 17, 2004, 1:52:45 PM”
You can either use a set of standard formats (standard formats)…
StringFormat=f : “Saturday, April 17, 2004 1:52 PM”
StringFormat=g : “4/17/2004 1:52 PM”
StringFormat=m : “April 17”
StringFormat=y : “April, 2004”
StringFormat=t : “1:52 PM”
StringFormat=u : “2004-04-17 13:52:45Z”
StringFormat=o : “2004-04-17T13:52:45.0000000”
… or you can create your own date formatting using letters (custom formats)
StringFormat=’MM/dd/yy’ : “04/17/04”
StringFormat=’MMMM dd, yyyy g’ : “April 17, 2004 A.D.”
StringFormat=’hh:mm:ss.fff tt’ : “01:52:45.000 PM”
MDSN article for standard date formatting
MSDN article for custom date formatting
Sample App
- Silverlight 4 Binding and StringFormat in XAML
- WPF在XAML中Binding使用StringFormat属性
- Silverlight 4常用StringFormat格式总结
- Silverlight 4常用StringFormat格式总结
- Silverlight 4常用StringFormat格式总结
- Binding StringFormat 笔记
- Silverlight/wpf DataGrid TextBox 等控件内容 Binding 中日期格式 StringFormat
- Silverlight之StringFormat、IValueConverter
- Silverlight 4 Binding Cheatsheet
- XAML入门-Silverlight 1.1 In Action
- Element to Element Binding in Silverlight
- Silverlight Binding
- windows phone- xaml- binding
- WPF: Animations in XAML and code-behind
- WPF: Animations in XAML and code-behind
- multiple xaml file in a single Silverlight application
- Silverlight 4中XAML解析的变化
- Silverlight 4中XAML解析的变化
- 实例解析:Oracle10g两个监听进程的故障
- sybase错误:JZ006: 捕获到 IO 例外
- Babs’ Box Boutique DFS()
- get提交 取不到百分号参数
- 使用 DataAdapter 执行批量更新
- Silverlight 4 Binding and StringFormat in XAML
- SWT/JFACE 在Tableviewer中只在表头上右击显示上下文菜单
- xp系统设置密码自动登陆
- jquery异步传输json数据格式实例
- java.lang.AbstractMethodError: org.apache.xerces.dom.DocumentImpl.setXmlVersion
- win7 C盘瘦身
- Java接口
- matlab code 对齐 以及 vs 2008 等的代码编辑与对齐
- Android设置多个定时器时只有最后一个定时器有效的问题