question

MPR-7304 avatar image
0 Votes"
MPR-7304 asked LeonLu-MSFT commented

How to Reload Data in ListViews?

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.

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

LeonLu-MSFT avatar image
0 Votes"
LeonLu-MSFT answered LeonLu-MSFT commented

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.




· 1
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.

@MPR-7304 May I know if you have got any chance to check my answer? I am glad to help if you have any other questions

0 Votes 0 ·