首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >向数据库插入新记录时,WPF MVVM ListView未更新

向数据库插入新记录时,WPF MVVM ListView未更新
EN

Stack Overflow用户
提问于 2019-10-09 04:38:35
回答 1查看 398关注 0票数 0

我在WPF MVVM应用程序中的ListView遇到了一点小问题。我的项目使用带有Dapper-Contrib的Sqlite数据库文件。我可以成功添加新员工,但之后我的ListView不会更新。

我的代码看起来像这样: SqlRepository.cs (泛型函数)

代码语言:javascript
复制
public IList<TEntity> GetAll()
{
    return DbContext.Connection.GetAll<TEntity>().ToList();
}

ISqlRepository.cs (接口)

代码语言:javascript
复制
IList<TEntity> GetAll();

EmployeeViewModel.cs (我的主视图的ViewModel)

代码语言:javascript
复制
using Autofac;
using AutoMapper;
using Calendar.Commands;
using Calendar.Database.Entities;
using Calendar.Database.Repositories;
using Calendar.Models;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Windows;

namespace Calendar.ViewModels
{
    internal class EmployeeViewModel : ViewModelBase
    {
        private RelayCommand command;
        ObservableCollection<EmployeeEntity> m_lstEmployees = new ObservableCollection<EmployeeEntity>();
        /// <summary>
        /// Initializes a new instance of the EmployeeViewModel class.
        /// </summary>
        public EmployeeViewModel()
        {
            employee = new Employee();

        }
        public Employee employee
        {
            get;
            set;
        }
        public RelayCommand AddNewEmployee
        {
            get
            {
                this.command = new RelayCommand(SaveChanges);
                return this.command;
            }
        }
        public void SaveChanges()
        {
            var container = ContainerConfig.Configure();
            using (var scope = container.BeginLifetimeScope())
            {
                var test = scope.Resolve<IEmployeeRepository>();
                employee.FirstName = this.employee.FirstName;
                employee.LastName = this.employee.LastName;
                var config = new MapperConfiguration(cfg => cfg.CreateMap<Employee, EmployeeEntity>());
                var mapper = config.CreateMapper();
                var test99 = mapper.Map<EmployeeEntity>(employee);
                test.Add(test99);
            }
        }
        public ObservableCollection<EmployeeEntity> EmployeeList
        {
            get
            {
                var container = ContainerConfig.Configure();
                using (var scope = container.BeginLifetimeScope())
                {
                    var test = scope.Resolve<IEmployeeRepository>();
                    IList<EmployeeEntity> gugus = test.GetAll();
                    foreach (var item in gugus)
                    m_lstEmployees.Add(item);
                }
                return m_lstEmployees;
            }
        }
    }
}

MainWindow.xaml

代码语言:javascript
复制
<ListView ItemsSource="{Binding EmployeeList, UpdateSourceTrigger=PropertyChanged}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="FirstName" DisplayMemberBinding="{Binding FirstName}"/>
                    <GridViewColumn Header="LastName" DisplayMemberBinding="{Binding LastName}"/>
                </GridView>
            </ListView.View>
        </ListView>

我知道我需要一个ObservableCollection,这样PropertyChanged才能触发(EmployeeViewModel继承了包含InotifyPropertyChanged内容的ViewModelBase )。

在我的数据库中添加新条目时,似乎没有“反馈”。我错过了什么?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-09 16:57:18

尝试在将更改保存到数据库后调用OnPropertyChanged(nameof(EmployeeList)) (可能在SaveChanges()方法中)。XAML标记需要知道何时重新计算列表视图ItemSource绑定中的EmployeeList。我所说的OnPropertyChanged是指ViewModelBase中的一个方法,它从INotifyPropertyChanged调用PropertyChanged事件(您可以使用不同的名称)。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58293778

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档