Solved

Understanding the guts of Model-View-ViewModel

Posted on 2010-08-14
8
309 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 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

22 Experts available now in Live!

Get 1:1 Help Now