2 回答
![?](http://img1.sycdn.imooc.com/5333a0780001a6e702200220-100-100.jpg)
TA贡献1829条经验 获得超13个赞
和
this.BestFitness = new ObservableCollection<DataPoint>();
...
你正在替换完整ItemsSource的情节。由于RaisePropertyChangedEvent之后调用没有视图通知,因此绑定图将无法识别更改并且图不会更新其点。
有两种可能的解决方案:
1.RaisePropertyChangedEvent替换集合后通过调用使用INotifyPropertychanged。所以
public ObservableCollection<DataPoint> BestFitness { get; set; }
应该扩展到
private ObservableCollection<DataPoint> _BestFitness;
public ObservableCollection<DataPoint> BestFintess
{
get
{
return _BestFitness;
}
private set
{
_BestFitness = value;
RaisePropertyChangedEvent(nameof(BestFintess));
}
}
2. 不要替换整个 ObservableCollection。只需清除现有集合并再次使用它们。这意味着使用
this.BestFitniss.Clear();
代替
this.BestFitness = new ObservableCollection<DataPoint>();
两种解决方案都会通知视图有关更改和绘图将更新它的点而不使用InvalidateFlag.
请注意,ObservableCollection如本问题所述,需要使用 UI 线程更改 an 的项目。由于您正在使用其他线程来添加调用 UI 的值,例如
Application.Current.Dispatcher.BeginInvoke(() =>
{
BestFitness.Add(new DataPoint(count, args.BestFitness));
});
是必须的。
- 2 回答
- 0 关注
- 359 浏览
添加回答
举报