INotifyPropertyChanged event listener within respective class not firing on client side (silverlight)

Posted by Rob on Stack Overflow See other posts from Stack Overflow or by Rob
Published on 2012-11-26T05:00:15Z Indexed on 2012/11/26 5:03 UTC
Read the original article Hit count: 176

I'm trying to work out if there's a simple way to internalize the handling of a property change event on a Custom Entity as I need to perform some bubbling of the changes to a child collection within the class when the Background Property is changed via a XAML binding:

public class MyClass : INotifyPropertyChanged
{
        [Key]
        public int MyClassId { get; set; }

        [DataMember]
        public ObservableCollection<ChildMyClass> MyChildren { get; set; }

        public string _backgroundColor;

        [DataMember]
        public string BackgroundColor
        {
            get
            {
                return this._backgroundColor;
            }

            set
            {
                this._backgroundColor = value;

                if (this.PropertyChanged != null)
                {
                    this.PropertyChanged(this, new PropertyChangedEventArgs("BackgroundColor"));
                }
            }
        }

        public MyClass()
        {
            this.BackgroundColor = "#FFFFFFFF";

            this.PropertyChanged += MyClass_PropertyChanged;
        }

        public event PropertyChangedEventHandler PropertyChanged;


        void MyClass_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        //Do something here - fires client side but not server side
    }

}

I can listen to the event by externalizing it without any problems but it's an ugly way to handle something that want to set and forget inside my class e.g.:

public class SomeOtherClass
{

    public SomeOtherClass()
    {
        MyClass mc = new MyClass();
        mc.PropertyChanged += MyClass_PropertyChanged;
    }

    void MyClass_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
    {
        MyClass mc = (MyClass)sender;
        mc.UpdateChildren();
    }
}

© Stack Overflow or respective owner

Related posts about c#

Related posts about event-handling