PropertyChange basic wonderings

PropertyChange basic wonderings

Old forum URL: forums.lhotka.net/forums/t/5292.aspx


jh72i posted on Monday, August 25, 2008

Been having a bunch of problems lately - infragistics/hierarchical/etc. and one thing came to mind that I oddly enough hadn't thought about before.....

In my property Sets I call PropertyHasChanged which, in turn, calls OnPropertyChanged. Now, say I have a property "Name" I'll call PropertyHasChanged("Name") whenever it changes. But what happens if the changing of Name causes other values on the object to change?

This is a dumb example but say I have a property NumberOfCharactersInName. As Name changes so too does this. But no notification of this goes out so bound items may not update themselves.

This is problematic because:

1. I don't want to have to call multiple PropertyChangeds for every property because:
   a. I don't think  want a million events being raised - especially when things are in Lists, etc.
   b. I don't want every property to have to know about other properties that are dependent on them - kind of breaks the observation idea.

2. I don't, either, want to have to call PropertyChanged(null) because I'd basically have to call that always (see b. in 1. above).

Thoughts?

ajj3085 replied on Monday, August 25, 2008

Well, if something is only listening for changes in NumberOfCharactersInName, then you either have to raise a PropertyChanged event for that property specifically, or call OnUnknownPropertyChanged, which calls a property changed with an empty string for the property name.

OnUnknownPropertyChanged will just raise one event, addressing 1a.  As far as 1b is concerned, it's your BO that "knows" which properties are dependant on each other, so I don't see how that affects the observer pattern.  The observer doesn't know that changing name will cause property changed for NumberOfCharsInName.. it just knows that the event was raised for that property, or it wasn't. 

jh72i replied on Monday, August 25, 2008

Ok thanks for the clarification - I'd say (but could be wrong) that I'm not alone in raising events for only the property that has changed and not thinking what other properties are using the value of this because they must also raise events. My use of the word "observe" here was probably incorrect - in that example I was thinking mostly of calculated properties. But I do have a lot of parent observing child / object observing associated object scenarios which must get extra complicated i suppose.

Seems then that the safest thing to do is raise PropertyChanged(this, null) mostly - just hate the idea of writing code to say....Name changes so then x, y, z have probably changed too because it means that Name must know about those other properties rather than the other way around.

Thanks for the reply.

Copyright (c) Marimer LLC