对集合的更改不会更新 UI

Changes to collection do not update UI

我不明白为什么当我更新一个对象时,我绑定的控件没有更新。

数据最初显示良好,但是当我想刷新 UI 中显示的数据时,更新对象时没有任何反应。对象更新正常。 ViewModel 确实在所有字段上使用 INotifyPropertyChanged。

但是,如果我直接更新单个项目,我可以更新我的 UI。如下评论。

我想我在这里某处犯了一个小学生错误?

更新:我已将模型添加到问题中。虽然我了解答案,但我不了解如何实施。尝试实施集合更改事件但未成功。可以给我一些指点吗?

        public partial class CisArrivalsPanel : UserControl
            {
                private ApiDataArrivalsDepartures _theArrivalsDepartures;

                public CisArrivalsPanel()
                {
                    InitializeComponent();
                    _theArrivalsDepartures = new ApiDataArrivalsDepartures();

                    _theArrivalsDepartures = MakeQuery.LiveTrainArrivals("London Kings Cross");

                    this.DataContext = _theArrivalsDepartures;

                    ListBoxArr.ItemsSource = _theArrivalsDepartures.StationMovementList;            
                }


                void Reload()
                {

//This does not update the UI**

                    _theArrivalsDepartures = MakeQuery.LiveTrainArrivals("London Paddington");

//However this (when uncommented, and I comment out the above line) does update the UI**

                    //_theArrivalsDepartures.StationMovementList[0].OriginName = "test";
                    //_theArrivalsDepartures.StationMovementList[0].Platform = "0";
                    //_theArrivalsDepartures.StationMovementList[0].BestArrivalEstimateMins = "999";
                    //_theArrivalsDepartures.StationName = "test";
                }


                private void StationHeader_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
                {
                    Reload();

                    Debug.WriteLine(_theArrivalsDepartures.StationName);
                    foreach (var a in _theArrivalsDepartures.StationMovementList)
                    {
                        Debug.WriteLine(a.OriginName);
                        Debug.WriteLine(a.BestArrivalEstimateMins);
                    }
                }
            }

编辑:添加模型

public class ApiDataArrivalsDepartures : INotifyPropertyChanged
    {
        private string _stationName;
        [JsonProperty(PropertyName = "station_name")]
        public string StationName {
            get
            {
                return _stationName;
            }
            set
            {
                _stationName = value;
                NotifyPropertyChanged("StationName");
            } 
        }

       private List<StationListOfMovements> _stationMovementList;

        public List<StationListOfMovements> StationMovementList
        {
            get
            {
                return _stationMovementList;
            }
            set
            {
                _stationMovementList = value;
                NotifyPropertyChanged("StationMovementList");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
    }



 public class StationListOfMovements : INotifyPropertyChanged
    {


        private string _originName;
        [JsonProperty(PropertyName = "origin_name")]
        public string OriginName {
            get
            {
                return _originName;
            }
            set
            {
                _originName = value;
                NotifyPropertyChanged("OriginName");
            } 
        }

        [JsonProperty(PropertyName = "destination_name")]
        public string DestinationName { get; set; }

        private string _platform;
        [JsonProperty(PropertyName = "Platform")]
        public string Platform {
            get
            {
                return _platform;
            }
            set
            {
                _platform = value;
                NotifyPropertyChanged("Platform");
            }
        }


        private string _bestArrivalEstimateMins;
        [JsonProperty(PropertyName = "best_arrival_estimate_mins")]
        public string BestArrivalEstimateMins {
            get
            {
                return _bestArrivalEstimateMins;
            }
            set
            {
                _bestArrivalEstimateMins = value;
                NotifyPropertyChanged("BestArrivalEstimateMins");
            } 
        }


        public event PropertyChangedEventHandler PropertyChanged;

        private void NotifyPropertyChanged(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(property));
            }
        }
    }

您也应该更新 DataContextItemsSource

void Reload()
{

  //This does not update the UI**

   _theArrivalsDepartures = MakeQuery.LiveTrainArrivals("London Paddington");
   DataContext = theArrivalsDepartures;
   ListBoxArr.ItemsSource = _theArrivalsDepartures.StationMovementList;
}

这里有两篇文章与您的 collection 有关(技术上是三篇):

  1. 如果你想让 new collection 传播,collection 属性 必须提高 PropertyChanged(听起来像确实如此)
  2. 如果您希望 collection 上的 add/remove 传播,您需要使用实现 INotifyCollectionChanged 的 collection . ObservableCollection是个不错的选择。
  3. 如果您想要传播对容器中项目的更改,那么那些项目需要实施INotifyPropertyChanged并且引发 PropertyChanged 事件。

确保涵盖所有这些内容,并且所做的更改应该会如您预期的那样显示在 UI 上。

用于集合 ObservableCollection ,此 class 在集合发生更改时通知 ui 您的重新加载功能有效,因为所有字段上都有 PropertyChanged 包括这个 它通知 ui 并重新加载正确的集合