Solved

Understanding the guts of Model-View-ViewModel

Posted on 2010-08-14
8
310 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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
 
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

DevOps Toolchain Recommendations

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

Question has a verified solution.

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

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
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…

773 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