How to Reload Data in ListViews?

MPR 1 Reputation point
2022-09-17T23:25:56.407+00:00

I have an ObservableCollection, and a ListView bound to that collection. When I add or delete from the collection the ListView changes. But if I change an element of the collection (i.e., a field of one of the objects), the ListView doesn't change. Apparently changes are only additions and deletions, not modifications.

So, I need to be able to force the data to be reloaded. How can I do that? I know how to do so in other environments, I just call something like reloadData(), but in .NET MAUI I can't figure it out :-(

Any help would be greatly appreciated. I'd like to do everything in code rather than XAML, as I am more comfortable working in code.

Thanks in advance for any help.

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
2,866 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,571 Reputation points Microsoft Vendor
    2022-09-19T07:56:24.327+00:00

    Hello,

    You need to achieve INotifyCollectionChanged Interface for your Model
    For example, you have Model called MyModel, you need to change the value of name at the runtime. You can refer to the following code.

       public class MyModel: INotifyPropertyChanged  
           {  
              private string _name;  
              public string Name  
               {  
                   set  
                   {  
                       if (_name != value)  
                       {  
                           _name = value;  
                           OnPropertyChanged("Name");                    
                       }  
                   }  
                   get  
                   {  
                       return _name;  
                   }  
               }  
              public event PropertyChangedEventHandler PropertyChanged;  
               protected virtual void OnPropertyChanged(string propertyName)  
               {  
                   PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));  
               }  
           }  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.