I have trouble getting a binding done using the WPF.
I have a model class which is similar to this:
public class DataModel
{
private double _temp;
public double Temperature
{
set
{
_temp= value;
OnPropertyChanged("Temperature");
}
get { return this._temp; }
}
}
This model is derived from a class BaseDataModel
public abstract class BaseDataModel
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}
}
I now have a list of DataModel objects in another class called DataViewModel, the List is named “Values”. In a class above that class I have an UserControl that needs to de dynamically created at runtime, therefore binding is done in code behind like this :
somewhere above the list:
DataViewModel model = new DataViewModel();
and the binding itself:
curbinding = new Binding();
curbinding.Source = model.Values;
curbinding.Path = new PropertyPath("Temperature");
curbinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
myTextBox.SetBinding(TextBox.TextProperty, curbinding);
I know that PropertyChanged is fired, but the value of the text box is not updated. I just can’t figure out why? When I create the bindig the first time, the text of the text box is updated, but when I change the value of Temperature, nothing happens.
Did I not think of something?
I have to say, that the application has a lot of other classes and is more complex, but as I said, the value is updated once, and never again.
If you didn’t read the comments I simply forgot to implement INotifyPropertyChanged …