none
Add PropertyDescriptor ValueChanged support to class RRS feed

  • Question

  • Given a Class with a few properties on it, I am currently support INotifyPropertyChanged.  However, also in the process of developing this class model, I found myself wondering about the PropertyDescriptor.ValueChanged event.  I've used it before on the receiving end, trapping the ValueChanged Event to know that an objects property's value had changed.  I am now looking at the reverse, where i use TypeDescriptor.GetProperties(GetType(MyClass)) and the propertydescriptor's returned should support the Value Changed event.  How do I add that support?  Is it already covered by the INotifyPropertyChanged interface, or is it something else?

    Thanks

    Jaeden "Sifo Dyas" al'Raec Ruiner


    "Never Trust a computer. Your brain is smarter than any micro-chip."
    PS - Don't mark answers on other people's questions. There are such things as Vacations and Holidays which may reduce timely activity, and until the person asking the question can test your answer, it is not correct just because you think it is. Marking it correct for them often stops other people from even reading the question and possibly providing the real "correct" answer.
    Friday, April 30, 2010 12:51 PM

Answers

  • Hi Jaeden,

    in my understanding, the PropertyDescriptor uses INotifyPropertyChanged to determine when a property has changed. For example, the following code will never execute the event handler:

     

    class Program
    {
     static void Main(string[] args)
     {
      A a = new A();
      TypeDescriptor.GetProperties(typeof(A))[0].AddValueChanged(a, OnPropertyChanged);
    
      a.B = 1;
     }
    
     private static void OnPropertyChanged(object sender, EventArgs e)
     {
      // this code will never be executed
     }
    }
    
    public class A
    {
     public int B { get; set; }
    }

     

    On the other hand, the following code WILL execute the event handler:

     

    class Program
    {
     static void Main(string[] args)
     {
      A a = new A();
      TypeDescriptor.GetProperties(typeof(A))[0].AddValueChanged(a, OnPropertyChanged);
    
      a.B = 1;
     }
    
     private static void OnPropertyChanged(object sender, EventArgs e)
     {
      // this code will be executed
     }
    }
    
    public class A : INotifyPropertyChanged
    {
     public event PropertyChangedEventHandler PropertyChanged;
     private int _b;
     public int B
     {
      get { return _b; }
      set
      {
       _b = value;
       if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("B"));
      }
     }
    }

    I Hope that helped.

    Munir

     

    • Marked as answer by JaedenRuiner Thursday, June 3, 2010 5:51 PM
    Thursday, June 3, 2010 1:40 PM

All replies

  • Hi Jaeden,

    in my understanding, the PropertyDescriptor uses INotifyPropertyChanged to determine when a property has changed. For example, the following code will never execute the event handler:

     

    class Program
    {
     static void Main(string[] args)
     {
      A a = new A();
      TypeDescriptor.GetProperties(typeof(A))[0].AddValueChanged(a, OnPropertyChanged);
    
      a.B = 1;
     }
    
     private static void OnPropertyChanged(object sender, EventArgs e)
     {
      // this code will never be executed
     }
    }
    
    public class A
    {
     public int B { get; set; }
    }

     

    On the other hand, the following code WILL execute the event handler:

     

    class Program
    {
     static void Main(string[] args)
     {
      A a = new A();
      TypeDescriptor.GetProperties(typeof(A))[0].AddValueChanged(a, OnPropertyChanged);
    
      a.B = 1;
     }
    
     private static void OnPropertyChanged(object sender, EventArgs e)
     {
      // this code will be executed
     }
    }
    
    public class A : INotifyPropertyChanged
    {
     public event PropertyChangedEventHandler PropertyChanged;
     private int _b;
     public int B
     {
      get { return _b; }
      set
      {
       _b = value;
       if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs("B"));
      }
     }
    }

    I Hope that helped.

    Munir

     

    • Marked as answer by JaedenRuiner Thursday, June 3, 2010 5:51 PM
    Thursday, June 3, 2010 1:40 PM
  • Yes it did thanks. I was just trying to determine what "caused" the affect of the ValueChanged event triggering.  Knowing that it is intrinsic to the PropertyDescriptor and the INotifyPropertyChanged interface makes my life very simple.

    THanks

    J"SD"a'RR


    "Never Trust a computer. Your brain is smarter than any micro-chip."
    PS - Don't mark answers on other people's questions. There are such things as Vacations and Holidays which may reduce timely activity, and until the person asking the question can test your answer, it is not correct just because you think it is. Marking it correct for them often stops other people from even reading the question and possibly providing the real "correct" answer.
    Thursday, June 3, 2010 5:50 PM