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
- <ListView x:Name="LivCategories" ItemsSource="{Binding Categories_GetActive, UpdateSourceTrigger=PropertyChanged}"
- SelectedItem="{Binding SelectedCategory}" Grid.Row="0" Grid.Column="0">
- <ListView.View>
- <GridView>
- <GridViewColumn Header="Id" Width="50" DisplayMemberBinding="{Binding CategoryId}"/>
- <GridViewColumn Header="Navn" Width="250" DisplayMemberBinding="{Binding CategoryName}"/>
- <GridViewColumn Header="Global" Width="50" DisplayMemberBinding="{Binding CategoryIsGlobal}"/>
- <GridViewColumn Header="Project" Width="150" DisplayMemberBinding="{Binding ProjectName}"/>
- <GridViewColumn Header="ProjectId" Width="0" DisplayMemberBinding="{Binding ProjectId}" />
- </GridView>
- </ListView.View>
- </ListView>
My ViewModel
- public CategoryViewModel()
- {
- LoadCategories();
- LoadProjects();
- CmdSave = new MyICommand(SaveChanges);
- CmdSaveNew = new MyICommand(SaveNew);
- }
- private ObservableCollection<Category> _categories_GetAll;
- public ObservableCollection<Category> Categories_GetAll
- {
- get
- {
- return _categories_GetAll;
- }
- set
- {
- _categories_GetAll = value;
- OnPropertyChanged("Categories_GetAll");
- }
- }
-
-
- private ObservableCollection<Category> _categories_GetActive;
- public ObservableCollection<Category> Categories_GetActive
- {
- get
- {
- return _categories_GetActive;
- }
- set
- {
- _categories_GetActive = value;
- OnPropertyChanged("Categories_GetActive");
- }
- }
-
-
- private ObservableCollection<Category> _categories_GetInactive;
- public ObservableCollection<Category> Categories_GetInactive
- {
- get
- {
- return _categories_GetInactive;
- }
- set
- {
- _categories_GetInactive = value;
- OnPropertyChanged("Categories_GetInactive");
- }
- }
-
-
- public void LoadCategories()
- {
- DalCategory dalCategory = new DalCategory();
- var categories = dalCategory.GetCategories();
-
- if (categories != null)
- {
-
- List<Category> Categories_GetAllList = categories;
- Categories_GetAll = new ObservableCollection<Category>(Categories_GetAllList);
-
-
- List<Category> Categories_GetActiveList = categories.Where(x => x.CategoryIsObsolete == false).ToList();
- Categories_GetActive = new ObservableCollection<Category>(Categories_GetActiveList);
-
-
- List<Category> Categories_GetInactiveList = categories.Where(x => x.CategoryIsObsolete == true).ToList();
- Categories_GetInactive = new ObservableCollection<Category>(Categories_GetInactiveList);
- }
- }
In this method I add a row to the Categories_GetCative
- public void SaveNew()
- {
- ResetMessages();
-
- DalCategory dal = new DalCategory();
-
- if (!string.IsNullOrEmpty(CategoryName))
- {
- string name = CategoryName;
- bool global = CategoryIsGlobal;
- bool obsolete = CategoryIsObsolete;
- int projectId = ProjectId;
-
- Category cat = new Category();
- cat.CategoryName = CategoryName;
- cat.CategoryIsGlobal = CategoryIsGlobal;
- cat.CategoryIsObsolete = CategoryIsObsolete;
- cat.ProjectId = ProjectId;
-
- int categoryId = dal.NewCategory(cat);
-
- if (cat.CategoryIsObsolete == false)
- {
- Categories_GetAll.Add(cat);
- Categories_GetActive.Add(cat);
- }
- else
- {
- Categories_GetInactive.Add(cat);
- }
-
- MessageOk = "Kategorien er gemt";
- }
- else
- {
- MessageError = "Kategorinavn skal udfyldes";
- }
- }
Here I make my PropertyChanged
- public event PropertyChangedEventHandler PropertyChanged;
- public void OnPropertyChanged(string propertyName)
- {
- PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
- }