question

Anja-7727 avatar image
0 Votes"
Anja-7727 asked Anja-7727 answered

I need to update a list in every instance of the app. It is wpf with mvvm

Hi, I'm hoping for some help.

I'm running a wpf mvvm app. In this connection I have made 2 tables (many more but mention only the 2 involved) Environment and Environment Version

When I load the app, it retrieves data from the 2 tables (no problems there).

But when I go in and create a new enivronment and then select environment version (where I have a combo box with all active environments), it does not update this list. It's still the same list as when I launched the app.

In my EnvironementViewModel I have made the following addition to the SaveNew function, but it still does not show the correctness of the combo box.

 //Here I want to reload all sessions with the new Environment
                 LoadEnvironment();

So where do I miss something?

My ViewModel Look like this

 namespace ViewModel.Account
 {
     public class EnvironmentViewModel : INotifyPropertyChanged
     {
         public EnvironmentViewModel()
         {
             LoadEnvironment();
             CmdSave = new MyICommand(SaveChanges);
             CmdSaveNew = new MyICommand(SaveNew);
             CmdSaveInactive = new MyICommand(SaveInactiveChanges);
         }        
    
         #region Messages
         private string _messageError;
         public string MessageError
         {
             get => _messageError;
             set
             {
                 _messageError = value;
                 OnPropertyChanged("MessageError");
             }
         }
    
         private string _messageOk;
         public string MessageOk
         {
             get => _messageOk;
             set
             {
                 _messageOk = value;
                 OnPropertyChanged("MessageOk");
             }
         }
    
         private string _messageErrorInactive;
         public string MessageErrorInactive
         {
             get => _messageErrorInactive;
             set
             {
                 _messageErrorInactive = value;
                 OnPropertyChanged("MessageErrorInactive");
             }
         }
    
         private string _messageOkInactive;
         public string MessageOkInactive
         {
             get => _messageOkInactive;
             set
             {
                 _messageOkInactive = value;
                 OnPropertyChanged("MessageOkInactive");
             }
         }
         #endregion
    
         #region New
         public string EnvironmentName { get; set; }
         public bool EnvironmentIsGlobal { get; set; }
         public bool EnvironmentIsObsolete { get; set; }
         public int ProjectId { get; set; }
         public string ProjectName { get; set; }
    
         //Gem ny kategori
         public MyICommand CmdSaveNew { get; set; }
    
         public void SaveNew()
         {
             ResetMessages();
    
             DalEnvironment dal = new DalEnvironment();
    
             if (!string.IsNullOrEmpty(EnvironmentName))
             {
                 Environment env = new Environment
                 {
                     EnvironmentName = EnvironmentName,
                     EnvironmentIsGlobal = EnvironmentIsGlobal,
                     EnvironmentIsObsolete = EnvironmentIsObsolete
                 };                
    
                 int environmentId = dal.NewEnvironment(env);
    
                 env.EnvironmentId = environmentId;
    
                 if (env.EnvironmentIsObsolete == false)
                 {
                     Environments_GetAll.Add(env);
                     Environments_GetActive.Add(env);
                 }
                 else
                 {
                     Environments_GetInactive.Add(env);
                 }
    
                 //Here I want to reload all sessions with the new Environment
                 LoadEnvironment();
             }
             else
             {
                 MessageError = "Miljø skal udfyldes";
             }
         }
         #endregion
    
         #region Get
         //Environments
         //All
         private ObservableCollection<Environment> _Environments_GetAll;
         public ObservableCollection<Environment> Environments_GetAll 
         { 
             get
             {
                 return _Environments_GetAll;
             }
             set
             {
                 _Environments_GetAll = value;
                 OnPropertyChanged("Environments_GetAll");
             }
         }
    
         //Active
         private ObservableCollection<Environment> _Environments_GetActive;
         public ObservableCollection<Environment> Environments_GetActive
         {
             get
             {
                 return _Environments_GetActive;
             }
             set
             {
                 _Environments_GetActive = value;
                 OnPropertyChanged("Environments_GetActive");
             }
         }
    
         //Inactive
         private ObservableCollection<Environment> _Environments_GetInactive;
         public ObservableCollection<Environment> Environments_GetInactive
         { 
             get
             {
                 return _Environments_GetInactive;
             }
             set
             {
                 _Environments_GetInactive = value;
                 OnPropertyChanged("Environments_GetInactive");
             }
         }
    
         //Load Environments
         public void LoadEnvironment()
         {
             DalEnvironment dalEnvironment = new DalEnvironment();
             var Environments = dalEnvironment.GetEnvironments();
    
             if (Environments != null)
             {
                 //All
                 List<Environment> Environments_GetAllList = Environments;
                 Environments_GetAll = new ObservableCollection<Environment>(Environments_GetAllList);
    
                 //Active
                 List<Environment> Environments_GetActiveList = Environments.Where(x => x.EnvironmentIsObsolete == false).ToList();
                 Environments_GetActive = new ObservableCollection<Environment>(Environments_GetActiveList);
    
                 //Inactive
                 List<Environment> Environments_GetInactiveList = Environments.Where(x => x.EnvironmentIsObsolete == true).ToList();
                 Environments_GetInactive = new ObservableCollection<Environment>(Environments_GetInactiveList);
             }
         }
    
         #endregion
    
         #region Set
         //Opdater kategori
         public MyICommand CmdSave { get; set; }
         public void SaveChanges()
         {
             ResetMessages();
             if (SelectedEnvironment != null)
             {
                 DalEnvironment dalEnvironment = new DalEnvironment();
                 var Environments = dalEnvironment.GetEnvironments();
    
                 Environment Environment = SelectedEnvironment;
    
                 dalEnvironment.SetEnvironment(SelectedEnvironment);
    
                 bool oldIsObsolete = (bool)Environments.First(i => i.EnvironmentId == SelectedEnvironment.EnvironmentId)?.EnvironmentIsObsolete;
    
                 if (oldIsObsolete != SelectedEnvironment.EnvironmentIsObsolete)
                 {
                     //Aktiv
                     if (!oldIsObsolete)
                     {
                         var EnvironmentsList = Environments_GetActive.OrderBy(o => o.EnvironmentName).ToList();
    
                         var singleEnvironment = EnvironmentsList.SingleOrDefault(u => u.EnvironmentId == SelectedEnvironment.EnvironmentId);
    
                         Environments_GetInactive.Add(SelectedEnvironment);
                         var EnvironmentsInactiveList = Environments_GetInactive.OrderBy(o => o.EnvironmentName).ToList();
    
                         EnvironmentsList.Remove(SelectedEnvironment);
                         EnvironmentsList.RemoveAll(p => p.EnvironmentId == singleEnvironment.EnvironmentId);
    
                         this.Environments_GetActive = new ObservableCollection<Environment>(EnvironmentsList);
                         this.Environments_GetInactive = new ObservableCollection<Environment>(EnvironmentsInactiveList);
                     }
                 }                
    
                 MessageOk = "Miljøet er gemt";                
             }
             else
             {
                 MessageError = "Du skal vælge et Miljø, før du kan gemme";
             }
         }
         public MyICommand CmdSaveInactive { get; set; }
         public void SaveInactiveChanges()
         {
             ResetMessages();
             if (SelectedInactiveEnvironment != null)
             {
                 DalEnvironment dalEnvironment = new DalEnvironment();
                 var Environments = dalEnvironment.GetEnvironments();
    
                 //DalEnvironment dal = new DalEnvironment();
                 Environment Environment = SelectedInactiveEnvironment;
    
                 dalEnvironment.SetEnvironment(SelectedInactiveEnvironment);
    
                 bool oldIsObsolete = (bool)Environments.First(i => i.EnvironmentId == SelectedInactiveEnvironment.EnvironmentId)?.EnvironmentIsObsolete;
    
                 //inaktiv
                 if (oldIsObsolete != SelectedInactiveEnvironment.EnvironmentIsObsolete)
                 {
                     //Inaktiv
                     if (oldIsObsolete)
                     {
                         var EnvironmentsList = Environments_GetInactive.OrderBy(o => o.EnvironmentName).ToList();
    
                         var singleEnvironment = EnvironmentsList.SingleOrDefault(u => u.EnvironmentId == SelectedInactiveEnvironment.EnvironmentId);
    
                         Environments_GetActive.Add(SelectedInactiveEnvironment);
                         var EnvironmentsActiveList = Environments_GetActive.OrderBy(o => o.EnvironmentName).ToList();
    
                         EnvironmentsList.Remove(SelectedInactiveEnvironment);
                         EnvironmentsList.RemoveAll(p => p.EnvironmentId == singleEnvironment.EnvironmentId);
    
                         this.Environments_GetActive = new ObservableCollection<Environment>(EnvironmentsActiveList);
                         this.Environments_GetInactive = new ObservableCollection<Environment>(EnvironmentsList); 
                     }                    
                 }
    
                 MessageOkInactive = "Kategorien er gemt";
             }
             else
             {
                 MessageErrorInactive = "Du skal vælge en kategori, før du kan gemme";
             }
         }
         #endregion
    
         #region Delete        
         #endregion
    
         #region Helpers
            
         //Nulstiller beskeder
         public void ResetMessages()
         {
             MessageError = string.Empty;
             MessageOk = string.Empty;
             MessageOkInactive = string.Empty;
             MessageErrorInactive = string.Empty;
         }
         #endregion
    
         #region SelectedItem
         //Her sætter jeg selected Environment til opdatering af den valgte kategori
         private Environment _selectedEnvironment;
    
         public Environment SelectedEnvironment
         {
             get
             {
                 return _selectedEnvironment;
             }
    
             set
             {
                 _selectedEnvironment = value;
                 OnPropertyChanged("SelectedEnvironment");
             }
         }
    
         //Her sætter jeg selected inactive Environment til opdatering af den valgte inaktive kategori
         private Environment _selectedInactiveEnvironment;
    
         public Environment SelectedInactiveEnvironment
         {
             get
             {
                 return _selectedInactiveEnvironment;
             }
    
             set
             {
                 _selectedInactiveEnvironment = value;
                 OnPropertyChanged("SelectedInactiveEnvironment");
             }
         }
         #endregion
    
         #region INotifyPropertyChanged Members
         //Her finder jeg ud af om selve propertyen er ændret
         public event PropertyChangedEventHandler PropertyChanged;
    
         public void OnPropertyChanged(string propertyName)
         {
             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
         }
         #endregion
    
         #region Instance
         //Her finder jeg ud af om der er findes en instans af EnvironmentViewModel
         private static EnvironmentViewModel _instance = null;
         public static EnvironmentViewModel GetInstance()
         {
             if (_instance == null) _instance = new EnvironmentViewModel();
             return _instance;
         }
         #endregion
     }
 }

What Is important to me, is that every instance are updated with the new environment list. So I hope You can help me :-)

windows-wpf
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

Anja-7727 avatar image
0 Votes"
Anja-7727 answered

I have tried another way not so complicated. But ran into another problem, so I make a new question

Best regards
SImsen :-)

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.