I was really excited when I learned the usage of INotifyProperty Changed 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.
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:
And I would access it with something like:
I implemented the INotifyProperty Changed interface in the class StudentName too. But the change is still not getting reflected automatically.
IF INotifyPorperty Changed 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
I will explain my question with an example. I have a student class.
Code:
public class Student : INotifyPropertyChanged
{
const int MINIMUM_AGE = 1;
int rollNumber = 0;
int age = 0;
string studentName = string.Empty;
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
public Student() { }
public Student(string studentName, int age, int rollNumber)
{
this.StudentName = studentName;
this.StudentAge = age;
this.RollNumber = rollNumber;
}
public string Name
{
get
{
return this.studentName;
}
set
{
if (value != this.studentName)//guard clause
{
if (value.Trim() == string.Empty)
throw new Exception("Name assigned does not confirms to the format. Please assign a proper alphanumeric value.");
this.studentName = value;
this.DisplayPropertyChange("StudentName");
}
}
}
public int StudentAge
{
get
{
return this.age;
}
set
{
if (value != this.age)//guard clause
{
//any value lesss than minumum age goes to minimum ages.
if (value < MINIMUM_AGE)
this.age = MINIMUM_AGE;
else
this.age = value;
this.DisplayPropertyChange("StudentAge");
}
}
}
public int RollNumber
{
get
{
return this.rollNumber;
}
set
{
if (value != this.rollNumber)//guard clause
{
if (value < 0)
throw new Exception("Wrong value supplied. Only non negative integers allowed.");
this.rollNumber = value;
this.DisplayPropertyChange("RollNumber");
}
}
}
void DisplayPropertyChange(string propertyName)
{
if (null!=this.PropertyChanged)
{
this.PropertyChanged(this,new PropertyChangedEventArgs(propertyName));
}
}
}
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:
Code:
public class StudentName
{
string firstName = string.Empty;
string lastName = string.Empty;
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
public string FullName
{
get { return String.Concat(this.firstName," ",this.lastName); }
}
//Default
public StudentName()
{}
//Overloaded
public StudentName(string firstName, string lastName)
{
this.firstName = firstName;
this.lastName = lastName;
}
}
Code:
Student student=new Student();
string.Format("The student's name is {0}",student.Name.FullName);
IF INotifyPorperty Changed 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
Comment