Solved

Understanding the guts of Model-View-ViewModel

Posted on 2010-08-14
8
308 Views
Last Modified: 2013-12-17
I am new to Model-View-ViewModel and wonder if the .NET Framework triggers whether a property has changed it's value or not.

I see that inside the Set of properties, the

                base.OnPropertyChanged("IsSelected"); (for example)

is called.  If the value did not change, does the .NET Framework then ignore this event?

What confuses me is this line of code:

            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)

What ever makes PropertyChanged not null?

This part, if done by the .NET Framework, I can then understand.  Otherwise I do not see in the demo program where this is done...

Here is the demo I am using:
http://msdn.microsoft.com/en-us/magazine/dd419663.aspx


Thanks,
newbieweb

/// </summary>

        public event PropertyChangedEventHandler PropertyChanged;



        /// <summary>

        /// Raises this object's PropertyChanged event.

        /// </summary>

        /// <param name="propertyName">The property that has a new value.</param>

        protected virtual void OnPropertyChanged(string propertyName)

        {

            this.VerifyPropertyName(propertyName);



            PropertyChangedEventHandler handler = this.PropertyChanged;

            if (handler != null)

            {

                var e = new PropertyChangedEventArgs(propertyName);

                handler(this, e);

            }

        }

Open in new window

0
Comment
Question by:newbieweb
  • 4
  • 4
8 Comments
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 500 total points
ID: 33438596
PropertyChanged will be non-null when something subscribes to the event. This could either be you subscribing to the event yourself somewhere or, in this case your view will be subscribing to the event as part of the data binding mechanics.

So basically the snippet of code you have posted is checking to see if anything has requested to be notified when a property changes. If it is null then there are no subscribers.
0
 

Author Comment

by:newbieweb
ID: 33438602
Thank you!  That makes perfect sense to me.

How does one subscribe to the event?
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 500 total points
ID: 33438612
Well assuming you have an instance of the class available, and a method who's signature matches the PropertyChangedEventHandler delegate, then you would do something like:
YourClass obj = new YourClass();



// subscribe to the event

obj.PropertyChanged += new PropertyChangedEventHandler(obj_PropertyChanged);



// This is the method that will receive notification when the event is raised

void obj_PropertyChanged(object sender, PropertyChangedEventArgs e)

{

     // do something in response to the event

}

Open in new window

0
 

Author Comment

by:newbieweb
ID: 33438630
See, that's what is puzzling me about the code I posted.  I searched the whole project for

"new Property" with Match Whole Word not checked.

I got one instance:

var e = new PropertyChangedEventArgs(propertyName);


but if you look at the code that only gets called when this.PropertyChanged is not null.

if (handler != null)

I don't get it.  Unless in this demo the code is there for this but not used?
0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 500 total points
ID: 33438645
Your probably not actually going to find the code in your app. The hooking up of the event will be "under the hood" and will be somewhere in the WPF base library so you won't actually find the code.
0
 

Author Comment

by:newbieweb
ID: 33438656
Oh, I see. So based on the types of bindings I have made with controls, it may make these kinds of decisions?  if not that, then what kinds of things might influence it?
0
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 500 total points
ID: 33438662
Yes. The binding mechanisms will take care of hooking up the PropertyChanged event if it is needed.
0
 

Author Closing Comment

by:newbieweb
ID: 33438672
Thanks.  It's not so mysterious any more...
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
For a while now I'v been searching for a circular progress control, much like the one you get when first starting your Silverlight application. I found a couple that were written in WPF and there were a few written in Silverlight, but all appeared o…
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now