Share via


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

Comments

  • Anonymous
    May 19, 2008
    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

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

  • Anonymous
    May 19, 2008
    ASP.NET Integrated social networking in ASP.NET [Via: 100muses ] Sharepoint When to dispose of objects...

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

  • Anonymous
    May 19, 2008
    In WinForms exists something related?

  • Anonymous
    May 20, 2008
    Karl, decimals work fine and nullable values also seem fine :)

  • Anonymous
    May 20, 2008
    Apologies for the sparseness of my posting the last few weeks - work and life have been busy here lately

  • Anonymous
    May 20, 2008
    Seems that Binding is getting even more functionality than before :)

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

  • Anonymous
    May 23, 2008
    Mijn verontschuldigingen voor de weinige posts de laatste weken. Mijn werk en leven zijn enorm de druk

  • Anonymous
    May 27, 2008
    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?

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

  • Anonymous
    June 01, 2008
    Vous trouverez ici les principales syntaxes permettant de réaliser un Data Binding en WPF. Le but ici

  • Anonymous
    June 02, 2008
    Would it be too much trouble to post an example that actually works?

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

  • Anonymous
    June 04, 2008
    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?

  • Anonymous
    June 05, 2008
    Endelig! En af de nye features der kommer med WPF 3.5 SP1 er StringFormat. Lester har skrevet en fin

  • Anonymous
    June 15, 2008
    Apologies for the sparseness of my posting the last few weeks - work and life have been busy here lately

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

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