0

I'm new at MVVM and I have a problem with updating lists.

I have 2 windows and ListViews in it. They are connected to a property "Tasks". When I add a new row to my db I need to refresh ListViews. I've done it, but only for 1 window.

adding a new row to a db table

private void OnAddTaskExecuted(object p)
        {
            tasks tsk = new tasks()
            {
                taskname = "1",
                description = "",
                date = DateTime.Now,
                empID = 2
            };
            Core.db.tasks.Add(tsk);
            Core.db.SaveChanges();
            Tasks = new ObservableCollection<tasks>(Core.db.tasks); 
            //it updates only in the window from which I'm adding the row
        }

viewmodel ctor

public MainWindowViewModel()
        {
            AddTask = new RelayCommand(OnAddTaskExecuted, p => true);
            Tasks = new ObservableCollection<tasks>(Core.db.tasks);
        }

So after clicking a btn I have this situation. ListView updates only in window where I click, but not in another (the new tasks is the first one)img

P.S. I have 2 same windows, I just making a new same window by btn click. That is just for a test. I'm actually creating a big project with lots of pages in it, and I need to update every Collection that have tasks in it.

nsv
  • 19
  • 4

1 Answers1

-1

Your issue is that you have two different Window instances with separate instances of ObservableCollection<tasks> Tasks

There are several ways in dotnet to structure your data in a persistent manner. Here are some:

  1. Assuming both Windows are of the same type, the easiest way is to just make Tasks a static field.

  2. Create a separate class, eg. called "TaskManager", with a field Tasks, and use that. If TaskManager is a static class, you don't have to bother with organizing any instances. Alternatively, there are concepts like Singletons. You can also store your data, or objects containing your data inside App.xaml.cs, where you can then access it via Application.Current.SomeField

If you want to reuse the same Window instance instead of storing your data somewhere else, that really depends on how you navigate to your Window. If you open your Window with something like this...

MyWindow window = new MyWindow();
window.Show();

... then you'll have to store your WindowInstance in a persistent manner as explained above, instead of instanciating a new one with new MyWindow() every time you want to show it.

Shrimperator
  • 560
  • 3
  • 13
  • Okay, I'm using static class **TaskManager**, but how can i use PropertyChanged in it. I'm binding my listview to Binding Source={x:Static mg:TaskManager.Tasks}, and it doesn't update when I change the static field. I assume that I need to have a PropertyChanged in get of a field, but I can't use INotifyPropertyChanged in my static class. – nsv Nov 04 '21 at 10:54
  • there are some discussions on this, such as this one: [Binding static property and implementing INotifyPropertyChanged](https://stackoverflow.com/questions/4680244/binding-static-property-and-implementing-inotifypropertychanged) one solution would be a non-static property on the viewmodels that utilize the taskmanager, which you would actually bind to. The getter of said property can then just point to the static property while the setter raises the PropertyChanged. another link: [link](https://stackoverflow.com/questions/16984611/notify-viewmodel-on-change-in-static-model) – Shrimperator Nov 04 '21 at 11:00
  • So. I have a property in viewmodel in which I use static class public static class TaskManager { public static ObservableCollection _Tasks; } _viewmodel_ public ObservableCollection Tasks { get => TaskManager._Tasks; set { TaskManager._Tasks = value; OnPropertyChanged(); } } Am I doing it right? Cause I got the same result. My listview updates only in 1 window – nsv Nov 04 '21 at 11:50
  • You might want to open a separate thread for this with all the details. If your listview has a different value in an entirely different instance of your ViewModel, you're probably still working with different copies of your data - can't tell why though without seeing the code. The part you're posting seems mostly right (except for the OnPropertyChanged without a parameter - but the OnPropertyChanged is probably unnecessary here anyways. Usually you wouldn't directly set this property after all, but the static setter inside the TaskManager. So this setter probably doesn't even get called) – Shrimperator Nov 04 '21 at 12:16