Welcome to MSDN Blogs Sign in | Join | Help

Lester's WPF blog


Simple, easy & beautiful

News

WPF 3.5 SP1 feature: StringFormat

One of the new features in 3.5 SP1 is stringformat. The usage is pretty simple. So following are simple snippets showing its use

<TextBox Text="{Binding Path=Double, StringFormat=F3}"/>

<TextBox Text="{Binding Path=Double, StringFormat=Amount: {0:C}}"/>

<TextBox Text="{Binding Path=Double, StringFormat=Amount: \{0:C\}}"/>

<TextBox>

  <TextBox.Text>

    <Binding Path="Double" StringFormat="{}{0:C}"/>

  </TextBox.Text>

</TextBox>

 

<TextBox>

  <TextBox.Text>

    <MultiBinding StringFormat="{}{0:F2} = {1:D}">

      <Binding Path="Double" />

      <Binding Path="Date"/>

    </MultiBinding>

  </TextBox.Text>

</TextBox>

<TextBox>

  <TextBox.Text>

    <Binding Path="Date" StringFormat="{}{0:MM/dd/yyyy}"/>

  </TextBox.Text>

</TextBox>

<ListBox Background="Beige" ItemStringFormat="F3">

  <sys:Double>1.11122</sys:Double>

  <sys:Double>2.11345</sys:Double>

</ListBox>

<GroupBox Background="AliceBlue" Content="{Binding Path=Double}" ContentStringFormat="F4"

          Header="{Binding Path=Double}" HeaderStringFormat="F5"/>

<Label Content="{Binding Path=Double}" ContentStringFormat="{}{0:E2}"/>

<GridView>

  <GridViewColumn Header="Date"

           DisplayMemberBinding="{Binding XPath=Date, StringFormat=D}" />

 This feature makes life a lot more easier when it comes to formatting.. So have fun with it.

Share this post

 

Posted: Monday, May 19, 2008 5:24 PM by llester

Comments

karl1406 said:

Nice post.  Like that multi-binding example.

I have not tried the new SP1 yet because I'm doing Silverlight and don't want to give that up until the tools release.

Did you try this with the decimal data type instead of double?  

I had to write a special converter to handle the decimal data type because the "$" was blowing up the type converter for a decimal data type.  My converter strips out the "$" before the string to decimal conversion takes place.

Did you try this with nullable properties on the data object.  I wrote a series of converters to handle nullable data types.

Cheers,

Karl

# May 19, 2008 1:58 PM

Zuker On Foundations said:

Via this post . I won&#39;t elaborate so much about it, do read Lester&#39;s post in the link above.

# May 19, 2008 2:14 PM

Christopher Steen said:

ASP.NET Integrated social networking in ASP.NET [Via: 100muses ] Sharepoint When to dispose of objects...

# May 20, 2008 2:43 AM

Rudi Grobler said:

I know I am a little late on blogging about .NET 3.5 SP1, but here goes... This is a huge release...

# May 20, 2008 2:46 AM

Cornel said:

In WinForms exists something related?

# May 20, 2008 5:09 AM

llester said:

Karl, decimals work fine and nullable values also seem fine :)

# May 20, 2008 12:37 PM

Community Blogs said:

Apologies for the sparseness of my posting the last few weeks - work and life have been busy here lately

# May 21, 2008 1:25 AM

Dmitrij Zaharov said:

Seems that Binding is getting even more functionality than before :)

# May 21, 2008 7:16 AM

Rob Relyea - Xamlified said:

My latest links about WPF (Apps, Controls, 3.5sp1 beta, HowTo, for LOB), Silverlight, XAML and URLs WPF

# May 23, 2008 4:28 PM

Scott Guthrie's Blog in Dutch said:

Mijn verontschuldigingen voor de weinige posts de laatste weken. Mijn werk en leven zijn enorm de druk

# May 24, 2008 3:35 AM

Jon said:

This is good news. This is what I was looking for in the original WPF. Having to create an IValueConverter to do simple formatting was a pain. It looks like there are a few different ways to escape the {} characters?

# May 27, 2008 1:39 PM

Mike Hillberg's Blog on Wpf (.Net and Silverlight) said:

StringFormat is a new property in .Net 3.5 SP1, which is currently in Beta. See Scott’s blog for more

# May 29, 2008 5:31 PM

Frédéric Hamel said:

Vous trouverez ici les principales syntaxes permettant de réaliser un Data Binding en WPF. Le but ici

# June 1, 2008 4:31 PM

James Hurst said:

Would it be too much trouble to post an example that actually works?

# June 2, 2008 6:04 PM

Scott Guthrie's Blog in French said:

Mes excuses pour le peu de publications au cours des dernières semaines – le travail et la vie en générale

# June 3, 2008 12:42 AM

Christian said:

Hi!

I like the string.format feature. But there is one problem:

If the value you are binding to is null the formated string will show

{DependencyProperty.UnsetValue}.

I would expect it to show simply nothing.

Will that be fixed in the final release?

# June 4, 2008 9:01 AM

tomledk said:

Endelig! En af de nye features der kommer med WPF 3.5 SP1 er StringFormat. Lester har skrevet en fin

# June 5, 2008 9:54 PM

Readed By Wrocław NUG members said:

Apologies for the sparseness of my posting the last few weeks - work and life have been busy here lately

# June 15, 2008 11:25 AM

WPF Tools Blog said:

I wrote some postings about data binding in the past (see here , here and here ). It is a pretty cool

# September 26, 2008 7:53 AM

Zuker On Foundations said:

Via this post . I won&#39;t elaborate so much about it, do read Lester&#39;s post in the link above.

# January 15, 2009 8:46 AM
New Comments to this post are disabled
Page view tracker