Posts Tagged ‘ Magic string ’

Advanced INotifyPropertyChanged with an automatic dispatcher

In WPF it is really important that you do not fire the PropertyChanged and CollectionChanged Events from a different thread then its creator.
Usually you have to check each time if you do not have to Invoke.

I wrote an abstract class which implements the INotifyPropertyChanged interface and which always fires the event from the right thread.
So it is no problem if you change the value of a property from another thread because the event is fired from the right thread.

Another problem of the PropertyChangedEvent is the PropertyChangedEventArgs where you have to specify the name of the property which change.
If you missspell the name of the property it will not update the databinding. Such problems are really difficult to find.
In order to reduce the risk of writing the wrong name I just added a few lines of code which check if there is a property with the given name. If not it throws an exception.
You could improve this class using the extension method from this website: http://blog.decarufel.net/2009/07/how-to-use-inotifypropertychanged-type.html

This is my DispatchingPropertyChangedProperty class which implements INotifyPropertyChanged and add the extra funcionality explained above.

	public abstract class DispatchingPropertyChangedProperty : INotifyPropertyChanged
	{
		protected DispatchingPropertyChangedProperty()
		{
			_currentDispatcher = Dispatcher.CurrentDispatcher;
		}

		private Dispatcher _currentDispatcher;
    
		private delegate void NotifyPropertyChangedDeleagte(object sender, string name);

		/// 
		/// This method executes the PropertyChanged-Event with the name of the property
		/// 
		/// The sender of the event
		/// The string of the property whichs value changed
		public void NotifyPropertyChanged(object sender, string name)
		{
			if(sender == null || name == string.Empty)
				throw new NullReferenceException("You have to specify a sender and the name of the property which changed");
			
			//Check if the given name is valid
			if (sender.GetType().GetProperty(name) != null)
			{
				if (_currentDispatcher.CheckAccess())
				{
					//We are in the correct thread
					//Execute the PropertyChanged-Event
					var handler = PropertyChanged;
					if (handler != null)
						handler(sender, new PropertyChangedEventArgs(name));
				}
				else
					_currentDispatcher.Invoke(DispatcherPriority.DataBind, new NotifyPropertyChangedDeleagte(NotifyPropertyChanged), sender, name);
			}
			else
				throw new Exception("The given name does have a corresponding property");
		}

		public event PropertyChangedEventHandler PropertyChanged;
	}

This is a class (Person) which uses the DispatchingPropertyChangedProperty class

public class Person : DispatchingPropertyChangedProperty
	{
		private string _firstname;
		private string _lastname;
		private int _age;
		private bool _isMale;

		public string Firstname
		{
			get{return _firstname;}
			set
			{
				_firstname = value;
				NotifyPropertyChanged(this,"Firstname");
			}
		}

		public string Lastname
		{
			get { return _lastname; }
			set
			{
				_lastname = value;
				NotifyPropertyChanged(this,"Lastname");
			}
		}

		public int Age
		{
			get { return _age; }
			set
			{
				_age = value;
				NotifyPropertyChanged(this,"Age");
			}
		}

		public bool IsMale
		{
			get { return _isMale; }
			set
			{
				_isMale = value;
				NotifyPropertyChanged(this,"IsMale");
			}
		}
	}