Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Understanding the guts of Model-View-ViewModel

Posted on 2010-08-14
8
Medium Priority
?
319 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Give Your Engineering Team a Productivity Boost

Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.

 

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

Fill in the form and get your FREE NFR key NOW!

Veeam® is happy to provide a FREE NFR server license to certified engineers, trainers, and bloggers.  It allows for the non‑production use of Veeam Agent for Microsoft Windows. This license is valid for five workstations and two servers.

Question has a verified solution.

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

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…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

705 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