Windows Phone – Binding Converter

When I make a small application to read RSS feed on Windows Phone 7, I discover that in current version of windows phone 7 framework the attribute ValueConversionAttribute is not supported so I can not make a binding converter when working with XAML. Therefore I would like to make a reverse engineering to steal the source code of ValueConversionAttribute in .Net Framework 3.5 and use it in my application. Because I am lazy to invent a new example to demonstrate, I would like to use the example from this link http://learnwpf.com/Posts/Post.aspx?postId=05229e33-fcd4-44d5-9982-a002f2250a64 and adjust it so that it should work on Windows Phone 7 too.

Because the example is exactly same as the original one, I will tell which I changed to make it run on Windows Phone 7. First, the ObjectDataProvider is not provided in Windows Phone 7 therefore I must remove the DataContext of the ListBox and keep only databinding of ItemsSource and bind it with list of data objects

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
{
	ObservableCollection<SaleRecord> salesData = new ObservableCollection<SaleRecord>();
	salesData.Add(new SaleRecord(1001, System.DateTime.Now.Subtract(new TimeSpan(1, 1, 1)), 123.4m, 0.045f));
	salesData.Add(new SaleRecord(1002, System.DateTime.Now.Subtract(new TimeSpan(1, 0, 0)), 42m, 0.09f));
	salesData.Add(new SaleRecord(1003, System.DateTime.Now.Subtract(new TimeSpan(0, 58, 0)), 10.99m, 0.09f));

	lvData.ItemsSource = salesData;
}

and then remove the “sales” resource from resources section

<phoneNavigation:PhoneApplicationPage.Resources>
	<my:FormattingConverter x:Key="formatter" />
</phoneNavigation:PhoneApplicationPage.Resources>

As I told above, ValueConversionAttribute is not supported in Windows Phone 7. Therefore I must steal from .Net Framework 3.0 and it looks like following

[AttributeUsage(AttributeTargets.Class, AllowMultiple = true)]
public sealed class ValueConversionAttribute : Attribute
{
	// Fields
	private Type _parameterType;
	private Type _sourceType;
	private Type _targetType;

	// Methods
	public ValueConversionAttribute(Type sourceType, Type targetType)
	{
		if (sourceType == null)
		{
			throw new ArgumentNullException("sourceType");
		}
		if (targetType == null)
		{
			throw new ArgumentNullException("targetType");
		}
		this._sourceType = sourceType;
		this._targetType = targetType;
	}

	public override int GetHashCode()
	{
		return (this._sourceType.GetHashCode() + this._targetType.GetHashCode());
	}

	// Properties
	public Type ParameterType
	{
		get
		{
			return this._parameterType;
		}
		set
		{
			this._parameterType = value;
		}
	}

	public Type SourceType
	{
		get
		{
			return this._sourceType;
		}
	}

	public Type TargetType
	{
		get
		{
			return this._targetType;
		}
	}
}

To use it just put it above your class

[ValueConversion(typeof(object), typeof(string))]
public class FormattingConverter : IValueConverter
{
	...
}

In the code of ValueConversionAttribute, I removed a override property. The absence of this property can cause program to crash (maybe). So use it on your risk and wait for a new version of framework for Windows Phone 7. The complete source code you can download here “Windows Phone Binding Converter