Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Understanding the guts of Model-View-ViewModel

Posted on 2010-08-14
8
Medium Priority
?
321 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 2000 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 2000 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
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
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…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

886 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