views:

639

answers:

3

As you may know, Silverlight 3 doesn't support IMultiValueConverter and... I badly need it. A Web Service proxy which defines a class structure that I need to display in my UI. The object definition class has a few array property such as string[], int[], etc. When I bind these property to a TextBlock, the Text property of the TextBlock becomes System.String[] or System.Int[]. Instead, I would like to see a list strings or numbers separated by a comma.

I thought about using a IMultiValueConverter but Silverlight 3 doesn't support it. How do I work around this?

Thanks

A: 

I dont see the use of a Multivalue Converter in your scenario. You can create a IValueConverter which takes Array and return you the string comma separated

<TextBlock Text="{Binding ArrayProperty,Converter={StaticResource stringArrayToString}}" ...
Jobi Joy
+3  A: 

The purpose of IMultiValueConverter is to implement converters that support multiple bindings (i.e. MultiBinding objects). In your case, this doesn't actually seem to be what you need.

If you want to convert an array (string[] for example) into a text value, then simply define a normal IValueConverter that does that. Don't let the fact that an array contains multiple values confuse you.

Here's an example converter:

[ValueConversion(typeof(string[]), typeof(string))] 
public class StringArrayConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        return string.Join(", ", (string[])value);
    }

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

Hope that helps.

Noldorin
A: 

And if you still want to have MultiBinding and IMultiValueConverter, you can you this one by Colin Eberhardt. Works really fine.

dalind