Lester's WPF\SL Blog

Simple, Easy & Beautiful

WPF 3.5 SP1 feature: StringFormat

WPF 3.5 SP1 feature: StringFormat

Rate This
  • Comments 20

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

 

  • 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

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

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

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

  • In WinForms exists something related?

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

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

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

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

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

  • 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?

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

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

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

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

Page 1 of 2 (20 items) 12