Welcome to Pete Brown's 10rem.net

First time here? If you are a developer or are interested in Microsoft tools and technology, please consider subscribing to the latest posts.

You may also be interested in my blog archives, the articles section, or some of my lab projects such as the C64 emulator written in Silverlight.

(hide this)

More Uses for Value Converters and Extension Methods in Silverlight Binding

Pete Brown - 07 March 2009

I’m working on a super quick Silverlight 2 application for a restaurant kiosk. We’re using EF and Astoria / ADO.NET Data Services as our data provider, and to structure the client I’m using an implementation of MVVM based on Shawn Wildermuth’s great MSDN article showing how to implement simple MVVM using Silverlight 2. (the article is great in that it doesn’t go nuts trying to solve the command / trigger issue, it just makes the most of what is available and keeps things simple)

I love binding in Silverlight, but sometimes you need to do things with binding that you know how you would do in inline code, but maybe aren’t sure how to handle in binding. Nine times out of ten, a value converter can help you out.

For example, I have a DataGrid full of guests. The grid needs to show the full address for the customer. In order to make the most efficient use of screen space, I want to format that as a single cell, multi-line field. Of course, the data is modeled as separate fields for StreetAddress1, 2, City, State, Zip and Country.

Enter the value converter!

So I wrote a really quick value converter that takes in the whole object and returns back just the formatted address:

public class GuestSingleFieldAddressConverter : IValueConverter
{

    #region IValueConverter Members

    public object Convert(object value, Type targetType, 
                          object parameter, 
                          CultureInfo culture)
    {
        if (!(value is Guests))
            throw new ArgumentException("...", "value");

        Guests guest = (Guests)value;

        StringBuilder builder = new StringBuilder();
        if (!string.IsNullOrEmpty(guest.StreetAddress1)) 
            builder.AppendFormat("{0}\r\n", guest.Address1.Trim());
        if (!string.IsNullOrEmpty(guest.StreetAddress2)) 
            builder.AppendFormat("{0}\r\n", guest.Address2.Trim());

        builder.AppendFormat("{0}, {1} {2}\r\n", 
                                guest.City.TrimEvenNulls(), 
                                guest.State.TrimEvenNulls(), 
                                guest.ZipCode.TrimEvenNulls());
        
        if (!string.IsNullOrEmpty(guest.Country)) 
            builder.AppendFormat("{0}", guest.Country.Trim());


        return builder.ToString();
    }

    public object ConvertBack(object value, Type targetType, 
                              object parameter, 
                              CultureInfo culture)
    {
        throw new NotImplementedException();
    }

    #endregion
}

Very simple. You’ll notice that I also used a simple extension method to take care of formatting null strings:

public static class StringExtensions
{
    public static string TrimEvenNulls(this string source)
    {
        if (source == null)
            return string.Empty;
        else
            return source.Trim();
    }
}

To use this, I then create a Static Resource in my xaml pointing to the value converter (note this is also where my View Model reference lives. Page is a base class I have that inherits from UserControl and adds some UI helper code):

<pages:Page.Resources>
    <vm:GuestsViewModel x:Key="ViewModel" />
    <converters:GuestSingleFieldAddressConverter 
        x:Key="AddressConverter" />
</pages:Page.Resources>

And when binding in the data grid, simply bind to the whole object (don’t specify a path or include a comma) and specify the value converter static resource key:

<data:DataGridTextColumn Header="Mailing Address"
   Width="220"
   FontSize="11"
   Binding="{Binding Converter={StaticResource AddressConverter}}" 
    />

Value Converters don’t solve everything in binding, but they certainly do give you a ton of options for handling different situations. They’re really easy to build and use, plus, I just like having the code for them compartmentalized out in a separate class rather than some function in a screen, or a compound field polluting my model. It makes for cleaner code and easy binding.

   
posted by Pete Brown on Saturday, March 7, 2009
filed under:    

4 comments for “More Uses for Value Converters and Extension Methods in Silverlight Binding”

  1. Ben Hayatsays:
    Hey Pete, since you're using Astoria, Intersoft has developed a great client side Data Source for Astoria that will make your life a lot easier. They also have a great Data Presenter too. I've done research on this product and is really good. Here is the link:

    http://www.intersoftpt.com/ctp/

    Good luck.

    p.s. If you have any question, the CTO (Andry) is very good at responding.
  2. norMorIndussays:
    Hey friends!
    During times while our economy really was as appalling as at this moment, we be able to not carry out a large amount on the road to put away the wealth. One thing still was continuously promising: Seek for Nettie and Mickey, cause they might be our only persons, who can tell you in detail easy methods to make it! In case you require exclusive suggestion for painting or roofing proceed to [url=http://roofingburlesontexas.com]roofing in burleson texas[/url]. However still if everything would seem being magnificent plus you feel sound, bear in mind , there possibly will be things you might like to argue just with your Shrink or with me! I am Karl Mccormick and I am your neighboring adviser used for any and all trouble!

    BTW... have you ever been in the area of Texas? .. good Land!

Comment on this Post

Remember me

2 trackbacks for “More Uses for Value Converters and Extension Methods in Silverlight Binding”

  1. DotNetShoutoutsays:
    Thank you for submitting this cool story - Trackback from DotNetShoutout