将新项目添加到ObservableCollection时,UI仅更新一次

时间:2019-10-20 20:22:52

标签: c# wpf mvvm

一个小时前,我遇到了一个非常奇怪的错误,无法解决。我的代码包含一个ObservableCollection,它绑定到我的View(Listview)。我可以将一个新项目添加到我的数据库中,它也将添加到我的ObservableCollection中。用户界面正在正确更新,但只有第一次。如果我添加第二个项目,它将显示在我的数据库以及集合中,但是我的UI不再更新。谁能检查我的代码以查看是否有问题?

查看:

<ListView Name="Departments_Listview" ItemsSource="{Binding Departments, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding YourSelectedItem, Mode=TwoWay}" Height="346">
    <ListView.View>
        <GridView>
            <GridViewColumn Header="Department" DisplayMemberBinding="{Binding Department}"/>
        </GridView>
    </ListView.View>
</ListView>

ViewModel

using Autofac;
using Calendar.Commands;
using Calendar.Database.Entities;
using Calendar.Database.Repositories;
using Calendar.Helper_Classes;
using System.Collections.ObjectModel;
using System.Linq;
using System.Threading;
using System.Windows;

namespace Calendar.ViewModels
{
    public class DepartmentViewModel : ViewModelBase
    {
        private RelayCommand command;
        private DepartmentEntity _yourSelectedItem;
        ObservableCollection<DepartmentEntity> _Departments = new ObservableCollection<DepartmentEntity>();
        public DepartmentViewModel()
        {
        }
        public ObservableCollection<DepartmentEntity> Departments
        {
            get
            {
                var container = ContainerConfig.Configure();
                using (var scope = container.BeginLifetimeScope())
                {
                    var test = scope.Resolve<IDepartmentRepository>();
                    _Departments = test.GetAll().ToObservable();
                }
                return _Departments;
            }
            set
            {
                _Departments = value;
                NotifyPropertyChanged("Departments");
            }
        }

        private string department;

        public string Department
        {
            get { return department; }
            set
            {
                department = value;
                NotifyPropertyChanged("Department");
            }
        }
        public DepartmentEntity YourSelectedItem
        {
            get
            {
                return _yourSelectedItem;
            }
            set
            {
                if (value != null)
                {
                    Department = value.Department;
                }
                _yourSelectedItem = value;
                NotifyPropertyChanged("YourSelectedItem");
            }
        }
        private void NewDepartment()
        {
            var container = ContainerConfig.Configure();
            using (var scope = container.BeginLifetimeScope())
            {
                var NewDepartment = scope.Resolve<IDepartmentRepository>();
                DepartmentEntity newDepartment = new DepartmentEntity
                {
                    Department = "Bitte ändern"
                };
                NewDepartment.Add(newDepartment);
                int DepartmentId = NewDepartment.Count();
                _Departments.Add(
                    new DepartmentEntity()
                    {
                        Id = DepartmentId,
                        Department = "Bitte ändern"
                    });
            }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

在添加新项目后尝试添加NotifyPropertyChanged("Departments");,例如之后

 _Departments.Add(
     new DepartmentEntity()
     {
         Id = DepartmentId,
         Department = "Bitte ändern"
     });

它使UI能够了解Departments属性已更新。 您也可以考虑在Departments getter中删除该代码

var container = ContainerConfig.Configure();
using (var scope = container.BeginLifetimeScope())
{
      var test = scope.Resolve<IDepartmentRepository>();
      _Departments = test.GetAll().ToObservable();
}

因为您已经将新项目添加到_Departments支持字段