添加 Item 时,ObservableCollection<T> 不更新 ListView

ObservableCollection<T> does not update ListView when an Item is added

我在我的测试项目中使用了以下 ViewModel。我试图遵循 MVVM 模式。我需要视图中的即时更新功能。

public class StudentListViewModel : INotifyPropertyChanged
    {
        private string _name;
        private string _lastName;
        private int _age;
        private ObservableCollection<Student> _studentList;
        public string Name 
        {
            get => _name;
            set
            {
                _name = value;
                OnPropertyChanged("Name");
                InsertCommand.RaiseCanExecuteChanged();
            }
        }
        public string LastName
        { 
            get => _lastName;
            set
            {
                _lastName = value;
                OnPropertyChanged("LastName");
                InsertCommand.RaiseCanExecuteChanged();
            }
        }
        public int Age
        {
            get => _age;
            set
            {
                _age = value;
                OnPropertyChanged("Age");
                InsertCommand.RaiseCanExecuteChanged();
            }
        }
        public RelayCommand InsertCommand { get; }

        public StudentListViewModel()
        {
            InsertCommand = new RelayCommand((s) => InsertStudent(),
            () => !string.IsNullOrEmpty(Name) && !string.IsNullOrEmpty(LastName) && Age > 0);
            using (MyContext context = new MyContext())
            {
                studentList = new ObservableCollection<Student>(context.Students);
            };
        }
        public void InsertStudent()
        {
                Student st = new Student()
                {
                    Name = Name,
                    LastName = LastName,
                    Age = Age
                };
            using (var context = new MyContext())
            {
                context.Add(st);
                context.SaveChanges();
            }
        }
        public ObservableCollection<Student> studentList
        {
            get 
            {
                return _studentList;
            }
            set
            {
                _studentList = value;
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

问题是,当我向数据库添加条目时,它不会更新 ListView 并且只有在我重新运行应用程序时才能看到更改。第二个问题是,当我将光标焦点远离选定的文本框时,属性 发生了变化。是否可以在填写 属性 时更改 属性?

您在 studentList 中缺少 OnPropertyChanged:


public ObservableCollection<Student> studentList
        {
            get 
            {
                return _studentList;
            }
            set
            {
                _studentList = value;
                OnPropertyChanged(“studentList”);
            }
        }

编辑:

插入数据时需要重新加载列表:

public void InsertStudent()
        {
                Student st = new Student()
                {
                    Name = Name,
                    LastName = LastName,
                    Age = Age
                };
            using (var context = new MyContext())
            {
                context.Add(st);
                context.SaveChanges();
                studentList = new ObservableCollection<Student>(context.Students);
            }
        }

不需要像其他答案中那样重新加载整个集合。将单个项目添加到当前集合中:

public void InsertStudent()
{
    var st = new Student()
    {
        Name = Name,
        LastName = LastName,
        Age = Age
    };
    using (var context = new MyContext())
    {
        context.Add(st);
        context.SaveChanges();
    }
    studentList.Add(st);
}