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
"LivCategories" ItemsSource="{Binding Categories_GetActive, UpdateSourceTrigger=PropertyChanged}" - SelectedItem="{Binding SelectedCategory}" Grid.Row="0" Grid.Column="0">
-
-
-
"Id" Width="50" DisplayMemberBinding="{Binding CategoryId}"/> -
"Navn" Width="250" DisplayMemberBinding="{Binding CategoryName}"/> -
"Global" Width="50" DisplayMemberBinding="{Binding CategoryIsGlobal}"/> -
"Project" Width="150" DisplayMemberBinding="{Binding ProjectName}"/> -
"ProjectId" Width="0" DisplayMemberBinding="{Binding ProjectId}" />
My ViewModel
- public CategoryViewModel()
- {
- LoadCategories();
- LoadProjects();
- CmdSave = new MyICommand(SaveChanges);
- CmdSaveNew = new MyICommand(SaveNew);
- }
- private ObservableCollection
_categories_GetAll; - public ObservableCollection
Categories_GetAll - {
- get
- {
- return _categories_GetAll;
- }
- set
- {
- _categories_GetAll = value;
- OnPropertyChanged("Categories_GetAll");
- }
- }
- //Active
- private ObservableCollection
_categories_GetActive; - public ObservableCollection
Categories_GetActive - {
- get
- {
- return _categories_GetActive;
- }
- set
- {
- _categories_GetActive = value;
- OnPropertyChanged("Categories_GetActive");
- }
- }
- //Inactive
- private ObservableCollection
_categories_GetInactive; - public ObservableCollection
Categories_GetInactive - {
- get
- {
- return _categories_GetInactive;
- }
- set
- {
- _categories_GetInactive = value;
- OnPropertyChanged("Categories_GetInactive");
- }
- }
- //Load Categories
- public void LoadCategories()
- {
- DalCategory dalCategory = new DalCategory();
- var categories = dalCategory.GetCategories();
- if (categories != null)
- {
- //All
- List
Categories_GetAllList = categories; - Categories_GetAll = new ObservableCollection
(Categories_GetAllList); - //Active
- List
Categories_GetActiveList = categories.Where(x => x.CategoryIsObsolete == false).ToList(); - Categories_GetActive = new ObservableCollection
(Categories_GetActiveList); - //Inactive
- List
Categories_GetInactiveList = categories.Where(x => x.CategoryIsObsolete == true).ToList(); - Categories_GetInactive = new ObservableCollection
(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));
- }
Piyush PansuriyaPosted Apr 21, 2021, 8:05 AM