• Code
  • Tags
  • Users
  • Titles
  • Log in
  • Feedback
  • FAQ
Share Code
Welcome to ForkCan.com

ForkCan is all about sharing code in a social way.

Discuss, debate or argue with other devs about their or your own code.

Give other devs feedback or make a Fork (Make a better version of a shared code).

Rate the code, if you use the code mark it as used so others can see if the shared code is used by someone.

Help each other to be better devs and to be more productive.


Features not working yet:

Flag a post


QR Code

Tiny Url

http://4kcan.com/s/MTg5

Related Code
Fork of Strongly typed NotifyPropertyChanged
Silverlight RelayCommand to simplify using the ICommand interface
IsInDesignMode for WPF and Silverlight
Execute.OnUIThread & Execute.InBackground utility methods
Default command implementation for Silverlight

Strongly typed NotifyPropertyChanged

Strongly typed NotifyPropertyChanged using lambas

6
815 0 2 1 0 4

public class PropertyChangedBase : INotifyPropertyChanged
{
	public event PropertyChangedEventHandler PropertyChanged = delegate { };

	public void NotifyOfPropertyChange(string propertyName)
	{
			Execute.OnUIThread(() => PropertyChanged(this, new PropertyChangedEventArgs(propertyName)));
	}

	public void NotifyOfPropertyChange<TProperty>(Expression<Func<TProperty>> property)
	{
			var lambda = (LambdaExpression)property;

			MemberExpression memberExpression;
			if (lambda.Body is UnaryExpression)
			{
					var unaryExpression = (UnaryExpression)lambda.Body;
					memberExpression = (MemberExpression)unaryExpression.Operand;
			}
			else memberExpression = (MemberExpression)lambda.Body;

			NotifyOfPropertyChange(memberExpression.Member.Name);
	}
}

Usage:

public class SomeViewModel : PropertyChangedBase
{
		string name;

		public string Name
		{
				get { return name; }
				set
				{
						name = value;
						NotifyOfPropertyChange(() => Name);
				}
		}
}

Share: twitter | facebook   Action: used | fork | flag

c#

Mark 'c#' tag as 'like'

Mark 'c#' tag as 'ignore'

silverlight

Mark 'silverlight' tag as 'like'

Mark 'silverlight' tag as 'ignore'

wpf

Mark 'wpf' tag as 'like'

Mark 'wpf' tag as 'ignore'


 @torkelo
216
July 13, 2010 2:44 PM

Fork

 Strongly typed NotifyPropertyChanged -  @torkelo Tuesday 13, 2010 2:44 PM
 Fork of Strongly typed NotifyPropertyChanged -  @adam.lith Monday 03, 2011 1:28 PM


3 Feedback

Sweet, I've actually been looking for this one..! -  @CodingInsomnia Tuesday 13, 2010 9:36 PM
I'll definately use this one -  @vidarls Wednesday 14, 2010 1:45 PM
I think this stack-exchange [proposal](http://area51.stackexchange.com/proposals/11464/code-review?referrer=aWNm_Pdc -  greatwolf Sunday 16, 2011 9:38 AM

You must log in before you can give any feedback


4 Discussion(s)

Newest Oldest
0

One solution to avoid runtime checking and validation of properties, is by just doing it during development and debug mode only:

[Conditional("DEBUG")]
[DebuggerStepThrough]
private void ValidateProperty(string propertyName)
{
   if (this.GetType().GetProperty(propertyName) == null)
       throw new MissingMemberException(propertyName);
}
link | flag  | Reply

 @fredrikn "I'm the master"
3.11k
Tuesday 13, 2010 4:19 PM

0

The problem with this is that it doesn't force compile-time checking of properties that are on the class. Instead it's better to use your PropertyChangedBase to take a generic type to constrain the properties that are available: PropertyChangedBase<TObject>, then you end up with a NotifyPropertyChanged function which takes Expression<Func<TObject, object>> instead of an action that can return an expression. While this is still up for abuse it at least implies that you're expecting something that belongs to TObject.

Also I wouldn't assume that you want your event notification to fire on the UI thread. creation of new UI objects and removing old objects has to happen on the UI thread, but INotifyPropertyChanged isn't specific to WPF/Silverlight.

link | flag  | Reply

 @TheColonial
12
Saturday 17, 2010 2:07 PM

0

OJ, I agree that it would be nice to have compile-time checking against properties on the class, but your solution doesn't really do that either (as you say, it's still up for abuse).

NotifyOfPropertyChange(a => a.Name);

could just as easily be replaced with something stupid like this and still compile just fine:

NotifyOfPropertyChange(a => new Object());

Instead, maybe you could just use a convention to say that you should always use this:

NotifyOfPropertyChange(() => this.Name);

However, I have to agree that a => a.Name is slightly prettier than () => this.Name. Unfortunately it comes with a rather odd-looking class declaration that might confuse quite a bit (I wasn't even sure it would work to be quite honest):

public class MainViewModel : PropertyChangedBase<MainViewModel>

As you can see, I'm arguing with myself here.. sorry about that.. ;-)

link | flag  | Reply

 @CodingInsomnia "First one to share code"
349
Saturday 17, 2010 8:24 PM

0

Reply to: CodingInsomnia I think this stack-exchange proposal might be of interest to you. If it is show your support and help get it into beta :)

link | flag  | Reply

 greatwolf
22
Sunday 16, 2011 9:39 AM


You must log in before you can post a comment

Squeed
Made by: Fredrik Normén 2010