简体   繁体   English

我的列表框没有更新

[英]My listbox doesn't update

I have a listbox to which I'm binding HistoryItems , where HistoryItems is a ObservableCollection of History . 我有一个要绑定HistoryItems的列表框,其中HistoryItemsHistoryObservableCollection 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 : 这是History课:

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 : 当我启动该应用程序时,将填充列表,但是在一些关键子项上进行了一些修改后,回到主全景视图后,我尝试更新OnNavigatedToHistoryItems

 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? HistoryINotifyPropertyChanged ,而HistoryItemsObservableCollection<History>所以应该没有问题。什么导致列表不更新?

Since you are replacing HistoryItems when you refresh it doesn't matter that it's an ObservableCollection . 由于刷新时要替换HistoryItems ,因此它是ObservableCollection无关紧要。

You can either clear the HistoryItems and then add the new items when you refresh. 您可以清除HistoryItems ,然后在刷新时添加新项目。 Or the ViewModel should implement INotifyPropertyChanged and the HistoryItems setter should raise the event. 或者, ViewModel应该实现INotifyPropertyChangedHistoryItems设置程序应该引发事件。

Rewrite the setter for ViewModel.HistoryItems so that instead of doing this 重写ViewModel.HistoryItems的二传手,以便而不是这样做

_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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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