【发布时间】:2015-08-03 03:46:12
【问题描述】:
我有一个 ListView:
<ListView ItemsSource="{Binding Students, IsAsync=True}">
<ListView.View>
<GridView>
<!--Some view things -->
</GridView>
</ListView.View>
</ListView>
这是我的 ViewModel 中的Students:
public ICollection<Student> Students
{
get
{
return _students;
}
set
{
_students = value;
OnPropertyChanged(() => this.Students);
}
}
我正在像这样的一些任务中更新 Students(当用户在另一个 TreeView 中选择某些内容时我正在更新 SourceState):
private State _sourceState;
public State SourceState
{
get
{
return _sourceState;
}
set
{
_sourceState = value;
OnSourceStateChanged();
OnPropertyChanged(() => this.SourceState);
}
}
private void OnSourceStateChanged()
{
Task updateStudentFromSourceStateTask = new Task(() =>
{
Students = _stateService.GetAllStudents(_sourceState);
});
updateStudentFromSourceStateTask.Start();
}
问题是每当Students 发生变化时,UI 就会冻结,直到 ListView 在新的 Students 中显示数据,我不知道如何告诉 ListView 在后台线程中刷新它的数据(我试过 @987654329 @ 在绑定中,但没有帮助)
【问题讨论】:
-
您要一次性替换整个集合,这可能会影响 UI 性能。您如何将其切换到具有实现 INPC 的学生实例的 ObservableCollection。然后,当您希望更新它们时,您可以遍历新旧列表并根据需要进行更新(属性值、删除实例、添加新实例等)。
标签: c# wpf listview mvvm binding