ObservableCollection not updating View

前端 未结 3 1368
误落风尘
误落风尘 2020-12-01 21:15

I am just starting with MVVM and have hit a hurdle that I hope someone can help me with. I am trying to create a simple View with 2 listboxes. A selection from the first lis

3条回答
  •  爱一瞬间的悲伤
    2020-12-01 21:46

    You need to make your poco class within the ObservableCollection implement INotifyPropertyChanged.

    Example:

    
    .
    .
    .    
    
    
        
            
                
                    
                    
                    
                
            
        
    
    
    public class LocationViewModel : BaseViewModel
    {
        ObservableCollection _locations = new ObservableCollection();
        public ObservableCollection Locations
        {
            get
            {
                return _locations;
            }
            set
            {
                if (_locations != value)
                {
                    _locations = value;
                    OnNotifyPropertyChanged();
                }
            }
        }
    }
    
    public class Location : BaseViewModel
    {
        int _locationId = 0;
        public int LocationId
        {
            get
            {
                return _locationId;
            }
            set
            {
                if (_locationId != value)
                {
                    _locationId = value;
                    OnNotifyPropertyChanged();
                }
            }
        }
    
        string _name = null;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                if (_name != value)
                {
                    _name = value;
                    OnNotifyPropertyChanged();
                }
            }
        }
    
        float _latitude = 0;
        public float Latitude 
        { 
            get
            {
                return _latitude;
            }
            set
            {
                if (_latitude != value)
                {
                    _latitude = value;
                    OnNotifyPropertyChanged();
                }
            }
        }
    
        float _longitude = 0;
        public float Longitude
        {
            get
            {
                return _longitude;
            }
            set
            {
                if (_longitude != value)
                {
                    _longitude = value;
                    OnNotifyPropertyChanged();
                }
            }
        }
    }
    
    public class BaseViewModel : INotifyPropertyChanged
    {
        #region Events
        public event PropertyChangedEventHandler PropertyChanged;
        #endregion
    
        protected void OnNotifyPropertyChanged([CallerMemberName] string memberName = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(memberName));
            }
        }
    }
    

提交回复
热议问题