0
我無法使用WPF獲取綁定。數據綁定不會更新UI元素
我有一個模型類,這是與此類似:
public class DataModel
{
private double _temp;
public double Temperature
{
set
{
_temp= value;
OnPropertyChanged("Temperature");
}
get { return this._temp; }
}
}
這個模型是從類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);
}
}
}
我現在有DataModel的物體在被稱爲DataViewModel另一個類的列表,該列表被命名爲「值」。在該類上面一類我有一個需要去在運行時動態創建的用戶控件,因此綁定在代碼中完成的背後是這樣的:
以上列表中某個地方:
DataViewModel model = new DataViewModel();
,並結合本身:
curbinding = new Binding();
curbinding.Source = model.Values;
curbinding.Path = new PropertyPath("Temperature");
curbinding.UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged;
myTextBox.SetBinding(TextBox.TextProperty, curbinding);
我知道PropertyChanged被激發,但文本框的值不會更新。我只是無法弄清楚爲什麼?當我第一次創建bindig時,文本框的文本被更新,但是當我改變Temperature的值時,什麼都不會發生。 我沒有想到什麼?
我不得不說,應用程序有很多其他的類並且更復雜,但正如我所說的,值會更新一次,而不會再次更新。
正是你在哪裏執行INotifyPropertyChanged? – mtijn
我很愚蠢...我只是說,我accently刪除該部分,從來沒有想過它再次... soloution是從INotifyPropertyChanged派生像這樣:BaseDataModel:INotifyPropertyChanged 非常感謝你! :) – peer