__setattr__ is not clean and easy readable piece of code. That's why I dive into decorators and create a *notify_property*:

class notify_property(property):

   def __init__(self, getter):
       def newgetter(slf):
           #return None when the property does not exist yet
           try:
               return getter(slf)
           except AttributeError:
               return None
       super(notify_property, self).__init__(newgetter)

   def setter(self, setter):
       def newsetter(slf, newvalue):
           # do not change value if the new value is the same
           # trigger PropertyChanged event when value changes
           oldvalue = self.fget(slf)
           if oldvalue != newvalue:
               setter(slf, newvalue)
               slf.OnPropertyChanged(setter.__name__)
       return property(
           fget=self.fget,
           fset=newsetter,
           fdel=self.fdel,
           doc=self.__doc__)


The usage is the same as for @property - see details on my blog:
http://gui-at.blogspot.com/2009/11/inotifypropertychanged-in-ironpython.html

Unfortunately, this does not work in Silverlight - probably because such properties are not CLR fields. I'd like to find a way to make it work in Silverlight too.

--
-- Lukáš


Curt Hagenlocher wrote:
Why don't you think that __setattr__ is a good approach? The alternative (as shown in the linked Japanese-language blog) is to implement setters for the properties you care about and to manually trigger the event. 2009/11/6 Lukas Cenovsky <cenov...@bakalari.cz <mailto:cenov...@bakalari.cz>>

    Hi all,
    I was looking for WPF data binding in IronPython and I have found
    nice Japanese blog about it:
    
http://palepoli.skr.jp/wp/2009/06/28/wpf-listview-databinding-for-ironpython/

    It links to
    http://sdlsdk.codeplex.com/Thread/View.aspx?ThreadId=30322 that
    should have more information about how to implement
    INotifyPropertyChanged in IronPython which I am also interested
    in. Unfortunately this page is no more available. Does the
    guideline exist somewhere else? Thanks.

    PS: I know about
    
http://lists.ironpython.com/pipermail/users-ironpython.com/2009-August/010938.html
    but I don't think using __setattr__ is good approach. Also
    implementing the PropertyChanged event is different on the blog.

    --
    -- Lukáš
    _______________________________________________
    Users mailing list
    Users@lists.ironpython.com <mailto:Users@lists.ironpython.com>
    http://lists.ironpython.com/listinfo.cgi/users-ironpython.com


------------------------------------------------------------------------

_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

_______________________________________________
Users mailing list
Users@lists.ironpython.com
http://lists.ironpython.com/listinfo.cgi/users-ironpython.com

Reply via email to