ObservableCollection not noticing when Item in it changes (even with INotifyPropertyChanged)

前端 未结 18 2709
一生所求
一生所求 2020-11-22 02:06

Does anyone know why this code doesn\'t work:

public class CollectionViewModel : ViewModelBase {  
    public ObservableCollection Con         


        
18条回答
  •  太阳男子
    2020-11-22 02:49

    If i know ObservableCollection make event only when we add/delete or move items in our collection. When we simly update some properties in collection items collection don`t signalize about it and UI will not be updated.

    You can simly implement INotifyPropertyChange in your Model class. And than when we update some propery in collection item it automatically will update UI.

    public class Model:INotifyPropertyChange
    {
    //...
    }
    

    and than

    public ObservableCollection {get; set;}
    

    In my case i used ListView to Bind for this collection and in ItemTemplate set Binding to Model property and it work good.

    Here is some snippet

    Windows XAML :

    
        
    
    
        
            
            
        
        
            
                
                    
                        
                
            
        
        
            
                
                
            
            
                
                
                
            
            
    
    

    Model code example:

    public class PersonModel:INotifyPropertyChanged
    {
        public string Name
        {
            get => _name;
            set
            {
                _name = value;
                OnPropertyChanged();
            }
        }
    
        public int Age
        {
            get => _age;
            set
            {
                _age = value;
                OnPropertyChanged();
            }
        }
    
        private string _name;
        private int _age;
        //INotifyPropertyChanged implementation
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    

    And ViewModel implementation:

     public class ViewModel:INotifyPropertyChanged
    {
        public ViewModel()
        {
            Persons = new ObservableCollection
            {
                new PersonModel
                {
                    Name = "Jack",
                    Age = 30
                },
                new PersonModel
                {
                    Name = "Jon",
                    Age = 23
                },
                new PersonModel
                {
                    Name = "Max",
                    Age = 23
                },
            };
        }
    
        public ObservableCollection Persons { get;}
    
        public PersonModel SelectedPerson
        {
            get => _selectedPerson;
            set
            {
                _selectedPerson = value;
                OnPropertyChanged();
            }
        }
    
        //INotifyPropertyChanged Implementation
        public event PropertyChangedEventHandler PropertyChanged;
        protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    
        private PersonModel _selectedPerson;
    }
    

提交回复
热议问题