Quantcast
Channel: Xamarin.Forms — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 75885

How to use INotifyPropertyChanged with a ListView

$
0
0

I have a ListView whose ItemSource is an ObservableCollection of Message objects. Message objects implement INotifyPropertyChanged.

I am changing properties on a message object in the ObservableCollection but it isn't updating my UI (which is a custom rendered cell) until I scroll the list to hide, then show the updated item.

` public class Message :INotifyPropertyChanged
{
public Message ()
{
}

    public event PropertyChangedEventHandler PropertyChanged;

    public string subject { get; set; }
    public bool sent { get; set; }

    public void SetSent(bool value) {
        sent = value; 
        Device.BeginInvokeOnMainThread (() => {
            OnPropertyChanged ("sent");
        });
    }

    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
}`

This updates the ui:

messages_ = new ObservableCollection<Message> ();
messages_.Add(messageA);
messages_.Remove(messageB)

This Doesn't:

foreach(Message m in messages_) {
    m.SetSent(false);
}

Can anyone tell me why the UI doesn't update?

Furthermore the messages will eventually be grouped and the ItemSource changed to

messages_ = new ObservableCollection<GroupedMessage> ();

public class GroupedMessage : ObservableCollection<Message>
{
        public string date { get; set; }
}

Do I need to add INotifyPropertyChanged to the GroupedMessage class?

Thanks in advance


Viewing all articles
Browse latest Browse all 75885

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>