Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Conferences
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Windows Phone 7.5 Data Cookbook

You're reading from   Windows Phone 7.5 Data Cookbook Over 30 recipes for storing, managing, and manipulating data in Windows Phone 7.5 Mango applications.

Arrow left icon
Product type Paperback
Published in Oct 2011
Publisher Packt
ISBN-13 9781849691222
Length 224 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Ramesh Thalli Ramesh Thalli
Author Profile Icon Ramesh Thalli
Ramesh Thalli
Arrow right icon
View More author details
Toc

Converting data for display


On many occasions, we need to convert the data coming from the source into a form that is suitable for users to read. For example, we want to format a date as short date. In this recipe, let's add a date field to our CLR object and use the binding converter to display short date instead of long date.

Getting ready

For this recipe, let's copy the preceding project and name the new project as Recipe5. You can also do this by exporting the preceding project as a template and then creating the new project from this saved template.

How to do it...

  1. Open the DataClass.cs file from the solution window. Add a new property DateCreated:

    public class DataClass
    {
      public string Name { get; set; }
      public string Notes { get; set; }
      public int Priority { get; set; }
      public DateTime DateCreated { get; set; }
    }
  2. Add another class; as we are formatting the Date value, let's name it DateFormatter. In order for this class to inherit IValueConverter, you should use the System.Windows.Data namespace. Now add two functions Convert and ConvertBack:

    public class DateFormatter : IValueConverter
    {
      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
        string formatString = parameter as string;
        if (!string.IsNullOrEmpty(formatString))
        {
          return string. Format(culture, formatString, value);
        }
        return value.ToString();
      }
      public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
      {
        throw new NotImplementedException();
      }
    }
  3. Open the MainPage.xaml and let's add the namespace at the top first as PhoneApplicationPage attribute.

    xmlns:local="clr-namespace:Recipe5">
  4. Now Add the Resource markup as shown in the following code snippet:

        <phone:PhoneApplicationPage.Resources>
            <local:DateFormatter x:Key ="FormatConverter" />
        </phone:PhoneApplicationPage.Resources>
  5. Now, add two TextBlock controls for DateCreated column inside the ListBox control as shown in the following code snippet. Notice how the Converter property is set to a static resource with a converter parameter.

    <TextBlock x:Name ="tbDateCreated" Text ="DateCreated:" Grid.Row="5" Grid.Column ="0" />
    <TextBlock x:Name="tbDateCreatedContent" Grid.Row="5" Grid.Column ="1" Text="{Binding DateCreated,Converter={StaticResource FormatConverter}, ConverterParameter=\{0:d\}}" /> 
  6. Add initialization data for the DateCreated field in the code behind the XAML file:

    myData.DateCreated = DateTime.Now;
  7. Press F5 to run the code and you should see the following results:

How it works...

You can see that the DateCreated field is shorter than before. Converters are used for displaying the correct format of the data for users. In this recipe, DateFormatter uses string format to convert the date format. Similarly, various other conversions such as currency and percentage can be performed.

There's more...

You can learn deeper concepts related to data binding using this online resource:

http://msdn.microsoft.com/en-us/library/ms752039.aspx

See also

Check the recipes titled DataContext and How DataMode is used in this chapter.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image