(X) Hide this Upcoming webinar on Feb 23rd, 10 am PST (see your local time): Building Line-of-business Applications with Silverlight & WCF Data Services
More webinar info | Register | Other webinars
Tweet @silverlightshow and win a SilverlightShow Tweet-shirt. Learn how >>
Skip Navigation LinksHome / Tips / View Tip

Tip: How to format a binding value using converters?

+ Add to SilverlightShow Favorites
1 comments   /   posted by Martin Mihaylov on Sep 09, 2008
(1 votes)
Categories: Binding

First let's explain what the converters can be used for. Imagine you bind to an object's property, but the property is not formatted to your likings. In this case you can use converters. For example we bind to the PublishDate property of a Book object and want the date to be formatted like this - "dd MMM, yyyy".

Book myBook = new Book();
myBook.PublishDate = DateTime.Now;

First let's create our converter. It's a class that implements System.Windows.Data.IValueConverter (using VisualStudio you can easily generate the structure of the class).

public class DateTimeConverter : System.Windows.Data.IValueConverter
    {
        public object Convert( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )
        {
            return ( ( DateTime )value ).ToString( "dd MMM, yyyy" );
        }
 
        public object ConvertBack( object value, Type targetType, object parameter, System.Globalization.CultureInfo culture )...
    }

We have a method Convert, which takes several parameters. The value parameter will be the value of the property that we want to convert. It's of type object so we cast it to DateTime and use the ToString method of the DateTime class to format it as we please. Let's now see how to use this converter in the Xaml. First declare the namespace of the converter:

<UserControl x:Class="LayoutExperiments.Page"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:LayoutExperiments">

My DateTimeConverter class is defined in the Page.cs file, but you can put anywhere you want. The next step is to add the Resource to the UserControl of type DateTimeConverter:

<UserControl.Resources>
    <local:DateTimeConverter x:Key="DateTimeConverter" />
</UserControl.Resources>

And the last thing is to configure the binding and add the converter to it:

<TextBlock x:Name="MyText" Text="{Binding PublishDate, Converter={StaticResource DateTimeConverter}}"></TextBlock>

This can also be done entirely in C# using the System.Windows.Data.Binding class:

TextBlock myText = new TextBlock();
 
Book myBook = new Book();
myBook.PublishDate = DateTime.Now;
 
Binding binding = new Binding("PublishDate");
binding.Source = myBook;
binding.Converter = new DateTimeConverter();
 
myText.SetBinding( TextBlock.TextProperty, binding );
LayoutRoot.Children.Add( myText );

So thanks to this converter the value of the TextBlock will be "09 Sep, 2008" instead of "9/9/2008 3:31:PM".

That's it!

Share


Comments

Comments RSS RSS
  • RE: Tip: How to format a binding value using converters?  

    posted by Thayse on Feb 08, 2010 18:45
    Thanks, very good!

Add Comment

 
 

   
  
  
   
Please add 1 and 2 and type the answer here: