Silverlight 4 binding and stringformat in XAML

Source: Internet
Author: User
Tags silverlight

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. previusly, if I wanted to have a piece of text that said "Your name is [username]" I cocould either use the old Horizontal-Stack-Panel-And-2-TextBlocks trick (as seen
Below )...
<Stackpanel orientation = "horizontal">
<Textblock text = "your
Name is "margin =", "/>
<Textblock text = "{binding username}"/>
</Stackpanel>

... Or write a value converter (not going to be seen below because there's
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 wowould 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, #}%tively 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 & quot; {0} & quot;.': "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.82920.e + 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 shocould 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 "Hangzhou l 17,200 4, 1:52:45"

You can either use a set of standard formats (standard formats )...
Stringformat = F: "Saturday, Hangzhou l 17,200 4 PM"
Stringformat = G: "4/17/2004 PM"
Stringformat = m: "stringl 17"
Stringformat = Y: "listen l, 2004"
Stringformat = T: "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': "listen l 17,200 4 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 appsource: Silverlight 4 binding and stringformat in XAML

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.