簡體   English   中英

將新記錄插入數據庫時,WPF MVVM ListView 未更新

[英]WPF MVVM ListView not updating when inserting a new record to DB

我在 WPF MVVM 應用程序中的 ListView 有一點問題。 我的項目使用帶有 Dapper-Contrib 的 Sqlite 數據庫文件。 我可以成功添加新員工,但我的 ListView 之后不會更新。

我的代碼如下所示:SqlRepository.cs(通用函數)

public IList<TEntity> GetAll()
{
    return DbContext.Connection.GetAll<TEntity>().ToList();
}

ISqlRepository.cs(接口)

IList<TEntity> GetAll();

EmployeeViewModel.cs(我的主視圖的 ViewModel)

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;
            }
        }
    }
}

主窗口.xaml

<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 繼承 ViewModelBase,其中包含 InotifyPropertyChanged 的東西)。

向我的數據庫添加新條目時似乎沒有“反饋”。 我錯過了什么?

將更改保存到數據庫后(可能在SaveChanges()方法中)嘗試調用OnPropertyChanged(nameof(EmployeeList)) )。 XAML 標記在重新評估列表視圖ItemSource綁定中的EmployeeList時需要知道。 OnPropertyChanged我的意思是ViewModelBase中的一個方法,它從INotifyPropertyChanged調用PropertyChanged事件(您可能有不同的名稱)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM