Detecting PropertyChanged events

G

Guest

I have two classes (lets call them A and B). A and B will always be classes of identical types, although the class type is unknown at design time. What I need to do is update a property of B whenever the same property of A changes (but not update A when B changes). In other words, I need to be able to detect all of the PropertyChanged events of A and then, when an event occurs, set the corresponding property of B equal to the property of A. I imagine that this is similar to how a PropertyGrid monitors its SelectedObject(s)

Thanks for any help
Lance
 
B

Bill McCarthy

Hi Lance,

The property grid actually does not automatically refresh. What it does do is
uses an attribute on properties that tells it to refresh if it actually sets the
value. If however, you modify the value elsewhere, that is not automatically
reflected in the property grid.
There are a couple of approaches you can take, such as having the property's
only changeable via your control, or if you can modify the actual types, then
you can raise a PropertyChanged event that can be subscribed to. another pattern
used is the XXXChanged pattern, which you could wire into using reflection.

Bill.






Lance said:
I have two classes (lets call them A and B). A and B will always be classes
of identical types, although the class type is unknown at design time. What I
need to do is update a property of B whenever the same property of A changes
(but not update A when B changes). In other words, I need to be able to detect
all of the PropertyChanged events of A and then, when an event occurs, set the
corresponding property of B equal to the property of A. I imagine that this is
similar to how a PropertyGrid monitors its SelectedObject(s).
 
C

Charles Law

Hi Lance

How about ... create an OnPropertyChanged method in class A, which raises an
event PropertyChanged whenever a property is changed, i.e. call it from the
Set method of each property.

When you create class B object, add a handler for the corresponding class A
object for the PropertyChanged event. If you include the property name and
new value in the event args you can update the properties in class B.

HTH

Charles


Lance said:
I have two classes (lets call them A and B). A and B will always be
classes of identical types, although the class type is unknown at design
time. What I need to do is update a property of B whenever the same
property of A changes (but not update A when B changes). In other words, I
need to be able to detect all of the PropertyChanged events of A and then,
when an event occurs, set the corresponding property of B equal to the
property of A. I imagine that this is similar to how a PropertyGrid
monitors its SelectedObject(s).
 
G

Guest

you can raise a PropertyChanged event that can be subscribed to. another pattern used is the XXXChanged pattern, which you could wire into using reflection

That sounds possible. Thanks
 
G

Guest

Thanks for the idea, but I have over 1500 properties so I need something a little more automated.
 
P

Peter Huang

Hi Lance,

Based on my understanding, you have two class A and B,(Are the class A and
B you mention is a generic class or a component?), in your code you will
new the two classes and set each of them to a propertygrid. Then if we
change one of the property of A, we will change the according property of B.
If I misunderstand your meaning, please feel free to tell me.

I think if the two classes A and B are generic class(a plain class inherits
from object), to achieve your aim, we have to define an event and raise the
event in every set methods of properties of A but you can write just one
event handler function in the class B.

If the two classes are component, we can use the
IComponentChangeService.ComponentChanged.
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/
frlrfSystemComponentModelDesignIComponentChangeServiceClassComponentAddedTop
ic.asp


Look for the title in the link below on how to use the
IComponentChangeService.ComponentChanged
53.4 How do I control the state of my custom verb in my designer?
http://www.syncfusion.com/FAQ/WinForms/FAQ_c80c.asp

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top