question

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

Wpf Mvvm sort ObservableCollection doesn't work

Hi,
I have some ObservableCollection lists I'm adding and removing rows from.

When that is done (that works) I want to sort the list. This I do with this line

 Categories_GetActive.OrderBy(o => o.CategoryName).ToList();

I have tried with and without discard. But it does nothing at all. No errors either. I have tried to see the output - it shows me the list without order anything.

I hope that one of you can tell me what to do for order the ObservableCollection

Best regards
Simsen :-)

     My ObservableCollection
     private ObservableCollection<Category> _categories_GetActive;
             public ObservableCollection<Category> Categories_GetActive
             {
                 get
                 {
                     return _categories_GetActive;
                 }
                 set
                 {
                     _categories_GetActive = value;
                     OnPropertyChanged("Categories_GetActive");
                 }
             }

My method where I'm trying to make a OrderBy

 public void SaveChanges()
         {
             ResetMessages();
             if (SelectedCategory != null)
             {
                 DalCategory dalCategory = new DalCategory();
                 var categories = dalCategory.GetCategories();
    
                  Category category = SelectedCategory;
    
                 dalCategory.SetCategory(SelectedCategory);
    
                 bool oldIsObsolete = (bool)categories.First(i => i.CategoryId == SelectedCategory.CategoryId)?.CategoryIsObsolete;
    
                 if (oldIsObsolete != SelectedCategory.CategoryIsObsolete)
                 {
                     //Inaktiv
                     if (oldIsObsolete)
                     {
                         Categories_GetActive.Add(SelectedCategory);
                         Categories_GetActive.OrderBy(o => o.CategoryName).ToList();
    
                         Categories_GetInactive.Remove(SelectedCategory);
                     }
                     //Aktiv
                     else
                     {
                         Categories_GetInactive.Add(SelectedCategory);
                         _ = Categories_GetInactive.OrderBy(o => o.CategoryName).ToList();
    
                         Categories_GetActive.Remove(SelectedCategory);
                     }
                 }                
    
                 MessageOk = "Kategorien er gemt";                
             }
             else
             {
                 MessageError = "Du skal vælge en kategori, før du kan gemme";
             }
         }




dotnet-csharpwindows-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

PeterFleischer-3316 avatar image
0 Votes"
PeterFleischer-3316 answered Anja-7727 commented

Hi Anja,
you create a new sorted List (Categories_GetActive.OrderBy(o => o.CategoryName).ToList();) without assigning to _categories_GetActive. Please, try:


  this.Categories_GetActive = Categories_GetActive.OrderBy(o => o.CategoryName).ToList();
· 3
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.

When doing so, I get this errormessage I do not understand:

 Cannot implicitly convert type 'System.Collections.Generic.List<Model.Account.Category>' to 'System.Collections.ObjectModel.ObservableCollection<Model.Account.Category>'    ViewModel    
0 Votes 0 ·

ok, try this:

 this.Categories_GetActive = new ObservableCollection<Category>(Categories_GetActive.OrderBy(o => o.CategoryName));


1 Vote 1 ·
Anja-7727 avatar image Anja-7727 PeterFleischer-3316 ·

Thank you so very much. I'm so grateful (again) for your help :-)

0 Votes 0 ·