473,406 Members | 2,847 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

INotifyPropertyChanged Question.

59
I was really excited when I learned the usage of INotifyPropertyChanged interface. It made binding a bliss and the UI highly interactive and dynamic for me. But then with every problem comes a solution and more problems.
I will explain my question with an example. I have a student class.
Expand|Select|Wrap|Line Numbers
  1. public class Student : INotifyPropertyChanged
  2. {       
  3.         const int MINIMUM_AGE = 1;
  4.         int rollNumber = 0;
  5.         int age = 0;
  6.         string studentName = string.Empty;
  7.  
  8.         #region INotifyPropertyChanged Members
  9.         public event PropertyChangedEventHandler PropertyChanged;
  10.         #endregion
  11.  
  12.         public Student() { }
  13.         public Student(string studentName, int age, int rollNumber)
  14.         {           
  15.             this.StudentName = studentName;
  16.             this.StudentAge = age;
  17.             this.RollNumber = rollNumber;
  18.         }
  19.  
  20.         public string Name
  21.         {
  22.             get
  23.             {
  24.                 return this.studentName;
  25.             }
  26.             set
  27.             {
  28.                 if (value != this.studentName)//guard clause
  29.                 {
  30.                     if (value.Trim() == string.Empty)
  31.                         throw new Exception("Name assigned does not confirms to the format. Please assign a proper alphanumeric value.");
  32.                     this.studentName = value;
  33.                     this.DisplayPropertyChange("StudentName");
  34.                 }
  35.             }
  36.         }
  37.  
  38.         public int StudentAge
  39.         {
  40.             get
  41.             {
  42.                 return this.age;
  43.             }
  44.             set
  45.             {
  46.                 if (value != this.age)//guard clause
  47.                 {
  48.                     //any value lesss than minumum age goes to minimum ages.
  49.                     if (value < MINIMUM_AGE)
  50.                         this.age = MINIMUM_AGE;                                   
  51.                     else
  52.                         this.age = value;
  53.                      this.DisplayPropertyChange("StudentAge");
  54.                 }
  55.             }
  56.         }
  57.  
  58.         public int RollNumber
  59.         {
  60.             get
  61.             {
  62.                 return this.rollNumber;
  63.             }
  64.             set
  65.             {
  66.                 if (value != this.rollNumber)//guard clause
  67.                 {
  68.                     if (value < 0)
  69.                         throw new Exception("Wrong value supplied. Only non negative integers allowed.");
  70.                     this.rollNumber = value;
  71.                     this.DisplayPropertyChange("RollNumber");
  72.  
  73.                 }
  74.             }
  75.         }
  76.  
  77.         void DisplayPropertyChange(string propertyName)
  78.         {
  79.             if (null!=this.PropertyChanged)
  80.             {
  81.                 this.PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
  82.             }
  83.         }        
  84. }
So far so good. Binding to any of the property would reflect the change in the binded object.
Now, in the same class a property type is a complex/user defined type. Then how are we going to handle it.?e.g.
say in the above class the Name property is changed to the following class instead of string:

Expand|Select|Wrap|Line Numbers
  1. public class StudentName
  2. {
  3.         string firstName = string.Empty;
  4.         string lastName = string.Empty;
  5.  
  6.         public string FirstName
  7.         {
  8.             get { return firstName; }
  9.             set { firstName = value; }
  10.         }
  11.         public string LastName
  12.         {
  13.             get { return lastName; }
  14.             set { lastName = value; }
  15.         }
  16.  
  17.         public string FullName
  18.         {
  19.             get { return String.Concat(this.firstName," ",this.lastName); }
  20.         }
  21.         //Default
  22.         public StudentName()
  23.         {}
  24.         //Overloaded
  25.         public StudentName(string firstName, string lastName)
  26.         {
  27.             this.firstName = firstName;
  28.             this.lastName = lastName;
  29.         }
  30. }
And I would access it with something like:
Expand|Select|Wrap|Line Numbers
  1. Student student=new Student();
  2. string.Format("The student's name is {0}",student.Name.FullName);
I implemented the INotifyPropertyChanged interface in the class StudentName too. But the change is still not getting reflected automatically.
IF INotifyPorpertyChanged is not extendable for complex property is there some workaround?
Or am I overlooking some Object Oriented Concept here?
Any help will be highly appreciated.

Regards,
Sid
May 5 '10 #1
3 2477
ThatThatGuy
449 Expert 256MB
@babai28
You need to assign a method to the delegate of the PropertyChanged event...

something like this...
Expand|Select|Wrap|Line Numbers
  1. student.PropertyChanged+=new PropertyChangedEventHandler(student_PropertyChanged);
  2.  
and write the code to effect in that method
May 5 '10 #2
Christian Binder
218 Expert 100+
I don't know, if I understood your question correct, but I think you ment you won't be notified if a property of your property changes.?

E.g.
Expand|Select|Wrap|Line Numbers
  1. Student s = new Student();
  2. s.PropertyChanged += x;
  3. void x(...) {
  4.   //handle change
  5. }
  6.  
  7. s.Age = 99; // now x() would be called
  8. s.Name = new StudentName("John", "Doe"); // x() would be called
  9. s.Name.FirstName = "Jane"; // x() WON'T be called
  10.  
Is this your problem?
If yes, then you could solve it this way:

Student is IPropertyChanged, StudentName is IPropertyChanged too!

Expand|Select|Wrap|Line Numbers
  1. // in class student
  2. public StudentName StudentName {
  3.   get{
  4.   // ...
  5.   }
  6.  
  7.   set {
  8.    //detach from relayed event
  9.    _studentName.PropertyChanged -= RelayPropertyChange;
  10.  
  11.     _studentName = value;
  12.     OnNotifyPropertyChanged("StudentName");
  13.  
  14.     //If any changes are made in object of StudentName, RelayPropertyChange will be called
  15.     _studentName.PropertyChanged += RelayPropertyChange;
  16.   }
  17. }
  18.  
  19. void RelayPropertyChange(...) {
  20.   // here you can raise anoter propertychanged-event (of property "Name")
  21. }
  22.  
May 5 '10 #3
ThatThatGuy
449 Expert 256MB
@ChBinder
ya it doesnt change automatically like that....
you need to write code for it
May 10 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Marc Gravell | last post by:
Kind of an open question on best-practice for smart-client design. I'd really appreciate anyones views (preferably with reasoning, but I'll take what I get...). Or if anybody has any useful links...
13
by: Michael.Suarez | last post by:
Let's suppose I have an instance of MyClass, which has the properties MyText1 and MyText2, and sets the values of MyText1 and MyText2 to 'A' and 'B' in it's constructor. Let's also suppose I...
4
by: Betina | last post by:
Hi, I've created a Person-Object which implemts INotifyPropertyChanged. This Person-Object contains an Address-Object which implements INotifyPropertyChanged too. Then I create a...
4
by: Chuck Cobb | last post by:
I have a question regarding generics: Suppose I want to create some generic collection classes: Collection<Cats> c; Collection<Dogs> d; and both Cats and Dogs are inherited from a base class...
1
by: Chuck Cobb | last post by:
Here's one more question on this topic: I am developing a CRM system using business objects...I have used the approach we previously discussed to develop lists of business objects for the...
1
by: Hardeek Thakkar | last post by:
Dear friends, In my current project I am using the CustomCollections with the help of BindingList<Tgeneric class to store the database records instead using DataSet objects as offline database...
1
by: Gokul | last post by:
Hi, I'm using indexed properties in an object which acts as the binding source. How can I implement INotifyPropertyChanged for that object so that when the indexed property is updated, binding...
2
by: John Greenwood | last post by:
Hi, I'm not sure if i'm misunderstanding how INotifyPropertyChanged should work but it's not working as I expected. I have a sample program with a very simple 'Customer' class with three...
0
by: Gerrit | last post by:
Hello, On http://msdn2.microsoft.com/en-us/library/ms184414.aspx is an example of the implementation of INotifyPropertyChanged Interface. I work with the C# example. I have tried this sample,...
2
by: nelmr | last post by:
Okay say we have a class that implements INotifyPropertyChanged and a property that has code in the setter to raise the PropertyChanged event. On the GUI side we have a text box with the code: ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.