Click here to Skip to main content
15,892,298 members
Articles / Desktop Programming / WPF

WPF - Don't Block your UI

Rate me:
Please Sign up or sign in to vote.
4.92/5 (11 votes)
7 Jul 2011CPOL5 min read 70.1K   3.5K   40  
Using .NET 4 features to create a non-blocking user interface
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using System.Text;

namespace AsyncSample.Model
{

	public abstract class PropertyChangedHelper : INotifyPropertyChanged
	{
		public event PropertyChangedEventHandler PropertyChanged;
		
		public void OnPropertyChanged(string name)
		{
			PropertyChangedEventArgs args = new PropertyChangedEventArgs(name);
			OnPropertyChanged(args);
		}

		public void OnPropertyChanged(PropertyChangedEventArgs args)
		{
			if (PropertyChanged != null)
				PropertyChanged(this, args);
		}

		public static PropertyChangedEventArgs CreateArgs<T>(Expression<Func<T, Object>> propertyExpression)
		{
			return new PropertyChangedEventArgs(GetNameFromLambda(propertyExpression));
		}

		private static string GetNameFromLambda<T>(Expression<Func<T, object>> propertyExpression)
		{
			var expr = propertyExpression as LambdaExpression;
			MemberExpression member = expr.Body is UnaryExpression ? ((UnaryExpression)expr.Body).Operand as MemberExpression :  expr.Body as MemberExpression;
			var propertyInfo = member.Member as PropertyInfo;
			return propertyInfo.Name;
		}
	}

}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Russian Federation Russian Federation
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions