Anja Simonsen

Anja Simonsen

  • 2k
  • 41
  • 2.9k

Wpf mvvm ObservableCollection doesn't update listView

Apr 19 2021 4:12 PM
I want to update a listview in View when adding/removing an entry in viewmodel.
 
When running to curser I can see, that my Categories_GetActive is updated with row 4(5,6,7 and so on) but it doesn't show up in my ListView.
 
So I miss something (I think) in binding the ListView to the ObservableCollection in the ViewModel. But cann't see what.
 
When starting the page, the 3 rows from db is in the listview so I know I have the correct binding
 
Can anyone please help me to see, what I'm doing wrong?
 
The UpdateSourceTrigger=PropertyChanged in the View I added to see, if that would help to update but no.
 
If You want the full code, just say so. There is nothing to hide in my code :-)
 
Best regards
Simsen :-)
My Listview
  1. <ListView x:Name="LivCategories" ItemsSource="{Binding Categories_GetActive, UpdateSourceTrigger=PropertyChanged}"     
  2.                               SelectedItem="{Binding SelectedCategory}" Grid.Row="0" Grid.Column="0">    
  3.      <ListView.View>    
  4.          <GridView>    
  5.              <GridViewColumn Header="Id" Width="50" DisplayMemberBinding="{Binding CategoryId}"/>    
  6.              <GridViewColumn Header="Navn" Width="250"  DisplayMemberBinding="{Binding CategoryName}"/>    
  7.              <GridViewColumn Header="Global" Width="50"  DisplayMemberBinding="{Binding CategoryIsGlobal}"/>    
  8.              <GridViewColumn Header="Project" Width="150"  DisplayMemberBinding="{Binding ProjectName}"/>    
  9.              <GridViewColumn Header="ProjectId" Width="0"  DisplayMemberBinding="{Binding ProjectId}" />    
  10.           </GridView>    
  11.       </ListView.View>    
  12. </ListView>   
My ViewModel
  1. public CategoryViewModel()    
  2. {    
  3.     LoadCategories();    
  4.     LoadProjects();    
  5.     CmdSave = new MyICommand(SaveChanges);    
  6.     CmdSaveNew = new MyICommand(SaveNew);    
  7. }    
  8. private ObservableCollection<Category> _categories_GetAll;    
  9. public ObservableCollection<Category> Categories_GetAll     
  10. {     
  11.     get    
  12.     {    
  13.         return _categories_GetAll;    
  14.     }    
  15.     set    
  16.     {    
  17.         _categories_GetAll = value;    
  18.         OnPropertyChanged("Categories_GetAll");    
  19.     }    
  20. }    
  21.   
  22. //Active    
  23. private ObservableCollection<Category> _categories_GetActive;    
  24. public ObservableCollection<Category> Categories_GetActive    
  25. {    
  26.     get    
  27.     {    
  28.         return _categories_GetActive;    
  29.     }    
  30.     set    
  31.     {    
  32.         _categories_GetActive = value;    
  33.         OnPropertyChanged("Categories_GetActive");    
  34.     }    
  35. }    
  36.   
  37. //Inactive    
  38. private ObservableCollection<Category> _categories_GetInactive;    
  39. public ObservableCollection<Category> Categories_GetInactive    
  40. {     
  41.     get    
  42.     {    
  43.         return _categories_GetInactive;    
  44.     }    
  45.     set    
  46.     {    
  47.         _categories_GetInactive = value;    
  48.         OnPropertyChanged("Categories_GetInactive");    
  49.     }    
  50. }    
  51.   
  52. //Load Categories    
  53. public void LoadCategories()    
  54. {    
  55.     DalCategory dalCategory = new DalCategory();    
  56.     var categories = dalCategory.GetCategories();    
  57.   
  58.     if (categories != null)    
  59.     {    
  60.         //All    
  61.         List<Category> Categories_GetAllList = categories;    
  62.         Categories_GetAll = new ObservableCollection<Category>(Categories_GetAllList);    
  63.   
  64.         //Active    
  65.         List<Category> Categories_GetActiveList = categories.Where(x => x.CategoryIsObsolete == false).ToList();    
  66.         Categories_GetActive = new ObservableCollection<Category>(Categories_GetActiveList);    
  67.   
  68.         //Inactive    
  69.         List<Category> Categories_GetInactiveList = categories.Where(x => x.CategoryIsObsolete == true).ToList();    
  70.         Categories_GetInactive = new ObservableCollection<Category>(Categories_GetInactiveList);    
  71.     }    
  72. }   
In this method I add a row to the Categories_GetCative
  1. public void SaveNew()    
  2. {    
  3.     ResetMessages();    
  4.   
  5.     DalCategory dal = new DalCategory();    
  6.   
  7.     if (!string.IsNullOrEmpty(CategoryName))    
  8.     {    
  9.         string name = CategoryName;    
  10.         bool global = CategoryIsGlobal;    
  11.         bool obsolete = CategoryIsObsolete;    
  12.         int projectId = ProjectId;    
  13.   
  14.         Category cat = new Category();    
  15.         cat.CategoryName = CategoryName;    
  16.         cat.CategoryIsGlobal = CategoryIsGlobal;    
  17.         cat.CategoryIsObsolete = CategoryIsObsolete;    
  18.         cat.ProjectId = ProjectId;    
  19.   
  20.         int categoryId = dal.NewCategory(cat);    
  21.   
  22.         if (cat.CategoryIsObsolete == false)    
  23.         {    
  24.             Categories_GetAll.Add(cat);    
  25.             Categories_GetActive.Add(cat);                        
  26.         }    
  27.         else    
  28.         {    
  29.             Categories_GetInactive.Add(cat);    
  30.         }    
  31.   
  32.         MessageOk = "Kategorien er gemt";    
  33.     }    
  34.     else    
  35.     {    
  36.         MessageError = "Kategorinavn skal udfyldes";    
  37.     }    
  38. }  
Here I make my PropertyChanged
  1. public event PropertyChangedEventHandler PropertyChanged;  
  2. public void OnPropertyChanged(string propertyName)  
  3. {  
  4.     PropertyChanged?.Invoke(thisnew PropertyChangedEventArgs(propertyName));  
  5. }

Answers (1)