Silverlight – Data binding not listening for PropertyChanged event

data-bindingsilverlight

Could somebody please explain to me what happens here?
I am creating a binding in code.

The target object is a UserControl
The target property is a boolean DependencyProperty
The source object is a FrameworkElement and implements INotifyPropertyChanged
The source property is of type ObservableCollection

What happens:

  • The binding is created in code, the result BindingExpressionBase looks fine, the mode is OneWay, the target value gets set correctly (at this time)

    Binding b = new Binding();
    b.Path = "SourceProperty";
    b.Source = SourceObject;
    BindingExpressionBase e = this.SetBinding(TargetProperty, b);

  • The source property then gets changed as a result of another databinding. The UserControl tries to fire the PropertyChanged event.

  • ….but nobody is listening. PropertyChanged is null.

I am sure that nothing else is assigned to the target property, so it should still be bound. Why is the binding not listening for the PropertyChanged event?

Best Answer

Alright, I found the answer myself. This is a bug in Silverlight..

The code that does the following

if (PropertyChanged != null)  
{  
    PropertyChanged(this, new PropertyChangedEventArgs(propertyName));  
}  

must be directly on the class that you bind to, not on its ancestor. I had it inside a FirePropertyChanged() method on a base class and moving it to the derived class made the difference.

Related Topic