我一直在努力解决这个问题几天,但在某个地方,我显然走错了路。情况如下:我有一个带3个按钮的窗口(添加新任务,显示收件箱,今日显示)和Listview。我的TaskViewModel类有一个TaskModel的ObservableCollection,具有非常简单的Filter功能。我的课程如下:
public class TaskViewModel : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
{
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
public ObservableCollection<TaskModel> TaskCollection { get; private set; }
public TaskViewModel()
{
TaskDataAccess ac = new TaskDataAccess();
this.TaskCollection = ac.GetAllTasks();
}
public ICommand AddTaskCommand
{
get { return new DelegateCommand(this.AddTask); }
}
public ICommand FilterInboxCommand
{
get { return new DelegateCommand(this.FilterInbox); }
}
public void AddTask()
{
this.TaskCollection.Add(new TaskModel(9, "I", "New Item for testing"));
this.GetListCollectionView().Filter = this.IsInbox; ;
}
private void FilterInbox()
{
this.GetListCollectionView().Filter = this.IsInbox;
}
....
}
过滤器功能正常,但是当我调用新窗口“添加新任务”时,它不会更新列表视图(这里:this.TaskCollection.Add(new TaskModel(9,“I”,“New Item for testing”) “));
如果有人能给我一个暗示,我会很感激......
答案 0 :(得分:0)
尝试这样做......
创建一个私有字段(比如_taskCollection)来备份你的属性TaskCollection。
private readonly ObservableCollection<TaskModel> _taskCollection;
然后从TaskCollection属性中删除私有的setter。还要删除加载集合的构造函数代码。
而是以这种方式写出你的吸气剂......
public ObservableCollection<TaskModel> TaskCollection {
get {
if (this._taskCollection == null)
{
TaskDataAccess ac = new TaskDataAccess();
this._taskCollection = ac.GetAllTasks();
}
return this._taskCollection;
}
}
让我知道这种方式是否有效......