【发布时间】:2014-09-23 14:50:51
【问题描述】:
我有 2 个项目,一个包含我的模型,另一个包含我的视图(Windows 窗体)。 我试图刷新我的视图,特别是 Label 在我使用绑定的 build() 方法期间根据模型更改,但这不起作用。我不知道我的代码是错误的还是不可能的。
编辑:实际上,标签似乎需要 Update() 或 Refresh() 调用 才能在他的窗口中以图形方式更新...这可以解释我的问题
有我的模型类:
// ModelBuilder : INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private Substation currentSubstation;
public Substation CurrentSubstation
{
get
{
return this.currentSubstation;
}
set
{
if (value != this.currentSubstation)
{
this.currentSubstation = value;
NotifyPropertyChanged("CurrentSubstation");
}
}
}
private void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public void Build()
{
foreach (Uri substationUri in substationsUri)
{
Substation substation = new Substation(substationUri); // long process
this.CurrentSubstation = substation;
}
}
有我的看法
private void StartImportation_Click(object sender, EventArgs e)
{
this.model = new ModelBuilder();
// Old mistake:
//this.timeLabel.DataBindings.Add(new Binding("Text", this.model.CurrentSubstation,"name"));
this.timeLabel.DataBindings.Add(new Binding("Text", this.model, "CurrentSubstation.name"));
this.model.Build(); // I'd like to see the current substation created name
}
【问题讨论】:
-
在 CurrentSubstation 属性中,在 set 部分中,您需要像这样在函数中将属性名称作为参数传递 - NotifyPropertyChanged("当前变电站")
-
@KrishnrajRana 它仍然不起作用,但即使没有这个参数,我的代码也进入了 NotifyPropertyChanged 函数。我用断点检查了它。
-
它执行您的 NotifyPropertyChanged() 函数,因为您使用了可选参数。所以它永远不会抛出任何错误,因为它只是在函数内部传递空白字符串。所以你永远不会在目的地收到任何通知。
-
好吧,我会让你告诉我的。所以现在,我有了 NotifyPropertyChanged("CurrentSubstation");但仍然无法正常工作:/