Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to update propertiy in item list in ObservableCollection

aliZAKRIAaliZAKRIA USMember ✭✭✭

Hello gays i just want to ask how can i update ObservableCollection Mvvm pattern i tried so many solution but none of it work for me please any sugesstion below is my code :smile:

  public class Chat_MessageViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        private ObservableCollection<Chat_Message> _Chat_Messages;
        public ObservableCollection<Chat_Message> Chat_Messages {
            get { return _Chat_Messages; }
            set {
                _Chat_Messages = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Chat_Messages"));
                }
            }
        }

        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public Chat_MessageViewModel()
        {
            Chat_Messages = new ObservableCollection<Chat_Message>();       
        }

        public  Command<Chat_Message> UpdateItem
        {
            get
            {
                return new Command<Chat_Message>((chat_Message) => {
                    chat_Message.DeliDeliveryReadImg = "onecheck.png"; //this sholde be the update 
                    //Chat_Messages.Remove(chat_Message);
                });
            }

        }




        public void Chat_MessageViewModelList(int index)
        {
            try
            {

                App.hubConnection.On<List<Chat_Message>>("ChatListMes", (message) =>
                {
                    //do something on your ui maybe? 
                    if (message == null)
                    {
                        index = index - 1;
                    }
                    //bind the list to view ChMs.Chat_MessageViewModelList(message);
                    foreach (var l in message)
                    {
                        var myshorttimedate = l.TimeDateSent.ToString().Substring(0, 17);
                        _Chat_Messages.Add(
                        new Chat_Message
                        {
                            _id = l._id,
                            Msg = l.Msg,
                            chat_message_author_user_id = l.chat_message_author_user_id,
                            TimeDateSent = myshorttimedate,
                            DeliDeliveryReadImg = l.DeliDeliveryReadImg,
                            DeliveryStauts = l.DeliveryStauts,
                            ReadStauts = l.ReadStauts
                        });
                    }
                });

                Chat_Messages = _Chat_Messages;
            }
            catch (Exception ex)
            {
                throw ex;
            }


        }

    }
Tagged:

Answers

  • JoeMankeJoeManke USMember ✭✭✭✭✭

    Your Chat_Message class also needs to implement INotifyPropertyChanged.

    Side note, your Command getter is written incorrectly. As written, it will create a return a new Command object every time it is referenced. You should create a single instance and return it every time.

  • aliZAKRIAaliZAKRIA USMember ✭✭✭

    Hello joe thanks for your repay i did as told but data dose not lode after i inherit the INotifyPropertyChanged

        public class Chat_Message : INotifyPropertyChanged
        {
    
            public int _id { get; set; }
    
            public int ChatSesstionId { get; set; }
            public string Msg { get; set; }
            public string StartAndEnd { get; set; }
            public string BACKCOLOR { get; set; }
    
            public string chat_message_author_user_id { get; set; }
    
            public string chat_message_author_client_id { get; set; }
    
            public int Messequence { get; set; }
    
            public object TimeDateSent { get; set; }
    
            public string ImageLink { get; set; }
    
            public int DeliveryStauts { get; set; }
    
            public int ReadStauts { get; set; }
    
            public string DeliDeliveryReadImg
            {
    
                get { return DeliDeliveryReadImg; }
                set
                {
                    if(DeliDeliveryReadImg != null)
                    {
                        DeliDeliveryReadImg = value;
                        OnPropertyChanged("DeliDeliveryReadImg");
                    }
    
                }
            }
            public event PropertyChangedEventHandler PropertyChanged;
    
            protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
            {
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    
  • aliZAKRIAaliZAKRIA USMember ✭✭✭
    edited September 2019

    OK thanks i solve it
    this what work for me :

     private string _DeliDeliveryReadImg;
    
                    public string DeliDeliveryReadImg
                    {
    
                        get { return _DeliDeliveryReadImg; }
                        set
                        {
                            if(_DeliDeliveryReadImg != value)
                            {
                                _DeliDeliveryReadImg = value;
                                OnPropertyChanged("DeliDeliveryReadImg");
                            }
    
                        }
                    }
    
  • JoeMankeJoeManke USMember ✭✭✭✭✭
    edited September 2019

    That looks like a stack overflow exception waiting to happen. Where's your backing variable?

    Looks like you caught it before I saw the notification.

Sign In or Register to comment.