Announcement

All important changes of my site and my blog will be announced here.

Computer security

This is my hobby. I spend my freetime for it. I am not professional in this area. But I like it very much.

Everything Else

Which doesn’t belong to other categories will land here. It maybe about my daily life, entertainment, music, game … or something like that

Programming

This is my job. That means everyday I must sit before the monitor. Tip something, be annoyed by bugs,… but I simply like it.

Tutorial

When I found something interesting, I would like to share it to everyone. That is the reason for this category comes.

Home » Programming

Windows Phone – Binding Converter

Submitted by on Friday, 26 March 2010No Comment | 776 views

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

Popularity: 1% [?]

My strongly recommended books to read. Choose one and enjoy yourself.

Related Posts:

  • No Related Posts

Leave your response!

Add your comment below, or trackback from your own site. You can also subscribe to these comments via RSS.

Be nice. Keep it clean. Stay on topic. No spam.

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

This is a Gravatar-enabled weblog. To get your own globally-recognized-avatar, please register at Gravatar.