简体   繁体   中英

My listbox doesn't update

I have a listbox to which I'm binding HistoryItems , where HistoryItems is a ObservableCollection of History . Here is the listbox declaration :

 <ListBox x:Name="RecentListBox" SelectionChanged="RecentListBox_SelectionChanged"   ItemsSource="{Binding HistoryItems,Converter={StaticResource HistoryValueConverter} }" ItemsPanel="{StaticResource ItemsPanelTemplate1_Wrap}" ItemTemplate="{StaticResource RecentViewModelTemplate}">

Here is the History class :

public class History : INotifyPropertyChanged
    {
        public History() { }

        int _id;
        string _date;
        string _url;
        string _name;

        public History(int id, string date,string url,string name)
        {
            this.id = id;
            this.date = date;
            this.url = url;
            this.name = name;
        }

        public int id
        {
            get
            {
                return _id;
            }
            set
            {
                if (value != _id)
                {
                    _id = value;
                    NotifyPropertyChanged("id");
                }
            }
        }
        public string date
        {
            get
            {
                return _date;
            }
            set
            {
                if (!value.Equals(_date))
                {
                    _date = value;
                    NotifyPropertyChanged("string");
                }
            }
        }

        public string url
        {
            get
            {
                return _url;
            }
            set
            {
                if (!value.Equals(_url))
                {
                    _url = value;
                    NotifyPropertyChanged("url");
                }
            }
        }

        public string name
        {
            get
            {
                return _name;
            }
            set
            {
                if (!value.Equals(_name))
                {
                    _name = value;
                    NotifyPropertyChanged("name");
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;
        private void NotifyPropertyChanged(String propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (null != handler)
            {
              //  App.viewModel.HistoryItems = (App.Current as App).dataHandler.retrieveHistory_DB();
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }

When I start the app the list gets populated, but after I do some modifs in some pivot children, and go back to the main panorama view, I try to update the HistoryItems in OnNavigatedTo :

 App.ViewModel.HistoryItems = (App.Current as App).dataHandler.retrieveHistory_DB();

but the listbox doesn't get updated (and the function returns the correct data). What could the problem be? History is INotifyPropertyChanged and the HistoryItems is a ObservableCollection<History> so there should be no problem.. What is causing the list to not update?

Since you are replacing HistoryItems when you refresh it doesn't matter that it's an ObservableCollection .

You can either clear the HistoryItems and then add the new items when you refresh. Or the ViewModel should implement INotifyPropertyChanged and the HistoryItems setter should raise the event.

Rewrite the setter for ViewModel.HistoryItems so that instead of doing this

_historyItems = value;

it does this

if (_historyItems == null) 
  _historyItems = new ObservableCollection<HistoryItem>();
_historyItems.Clear();
foreach (var hi in value) 
  _historyItems.Add(hi);

你需要NotifyPropertyChangedApp.ViewModel中的二传手HistoryItems

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM