簡體   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