繁体   English   中英

我的列表框没有更新

[英]My listbox doesn't update

我有一个要绑定HistoryItems的列表框,其中HistoryItemsHistoryObservableCollection 这是列表框声明:

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

这是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));
            }
        }
    }

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

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

但列表框不会更新(该函数返回正确的数据)。 可能是什么问题? HistoryINotifyPropertyChanged ,而HistoryItemsObservableCollection<History>所以应该没有问题。什么导致列表不更新?

由于刷新时要替换HistoryItems ,因此它是ObservableCollection无关紧要。

您可以清除HistoryItems ,然后在刷新时添加新项目。 或者, ViewModel应该实现INotifyPropertyChangedHistoryItems设置程序应该引发事件。

重写ViewModel.HistoryItems的二传手,以便而不是这样做

_historyItems = value;

它做到了

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