Link to home
Start Free TrialLog in
Avatar of curiouswebster
curiouswebsterFlag for United States of America

asked on

Understanding the guts of Model-View-ViewModel

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

ASKER CERTIFIED SOLUTION
Avatar of Carl Tawn
Carl Tawn
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of curiouswebster

ASKER

Thank you!  That makes perfect sense to me.

How does one subscribe to the event?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks.  It's not so mysterious any more...