Moritz
Moritz

Reputation: 389

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

I've got a little problem with a ListView in a WPF MVVM application. My project uses a Sqlite database file with Dapper-Contrib. I can add a new employee successfully but my ListView won't update afterwards.

My code looks like this: SqlRepository.cs (generic function)

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

ISqlRepository.cs (Interface)

IList<TEntity> GetAll();

EmployeeViewModel.cs (ViewModel for my main view)

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

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

I know that I need an ObservableCollection so the PropertyChanged can trigger (EmployeeViewModel inherits ViewModelBase which contains the InotifyPropertyChanged stuff).

Seems that there is no "feedback" when adding a new entry to my database. What did I missed?

Upvotes: 0

Views: 617

Answers (1)

Pavel Anikhouski
Pavel Anikhouski

Reputation: 23298

Try to call OnPropertyChanged(nameof(EmployeeList)) after saving changes to database (probably in SaveChanges() method). XAML markup need to know when reevaluate EmployeeList in list view ItemSource binding. By OnPropertyChanged I mean a method in your ViewModelBase, which invokes PropertyChanged event from INotifyPropertyChanged (you can have a different name probably)

Upvotes: 1

Related Questions