(X) Hide this
    • Login
    • Join
      • Generate New Image
        By clicking 'Register' you accept the terms of use .

Tip: How to format a binding value using converters?

(1 votes)
Martin Mihaylov
>
Martin Mihaylov
Joined Oct 29, 2007
Articles:   50
Comments:   70
More Articles
1 comments   /   posted on Sep 09, 2008
Categories:   Data 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!


Subscribe

Comments

  • -_-

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


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

Add Comment

Login to comment:
  *      *