I have a DependencyObject with a DependencyProperty:
public class DependencyObjectClass: DependencyObject
{
public static DependencyProperty BooleanValueProperty = DependencyProperty.Register("BooleanValue", typeof (bool), typeof (DependencyObjectClass));
public bool BooleanValue
{
get { return (bool)GetValue(BooleanValueProperty); }
set { SetValue(BooleanValueProperty, value); }
}
}
I also have my data source class:
public class DataSource: INotifyPropertyChanged
{
private bool _istrue;
public bool IsTrue
{
get { return _istrue; }
set
{
_istrue = value;
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs("IsTrue"));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
I am trying to bind the two above objects with this code:
var dependencyObject = new DependencyObjectClass();
var dataSource = new DataSource();
var binding = new Binding("IsTrue");
binding.Source = dataSource;
binding.Mode = BindingMode.TwoWay;
BindingOperations.SetBinding(dependencyObject, DependencyObjectClass.BooleanValueProperty, binding);
Whenever I change the BooleanValue property on DependencyObjectClass, the DataSource does react, but it doesn't work the other way around (changing IsTrue property on DataSource does nothing for DependencyObjectClass).
What am I doing wrong? Do I have to manually handle the OnPropertyChanged event? If yes then that would be a bit disappointing, as I was expecting this to be done automatically.