我的列表框没有更新

时间:2011-10-25 15:39:03

标签: c# .net silverlight windows-phone-7

我有一个我正在绑定HistoryItems的列表框,其中HistoryItemsObservableCollection的{​​{1}}。  这是列表框声明:

History

以下是 <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)); } } } 中的HistoryItems

OnNavigatedTo

但列表框没有更新(并且函数返回正确的数据)。问题是什么? App.ViewModel.HistoryItems = (App.Current as App).dataHandler.retrieveHistory_DB(); HistoryINotifyPropertyChangedHistoryItems,因此应该没有问题。是什么导致列表无法更新?

3 个答案:

答案 0 :(得分:4)

由于您在刷新时替换HistoryItems,因此它与ObservableCollection无关。

您可以清除HistoryItems,然后在刷新时添加新项目。或者ViewModel应该实现INotifyPropertyChanged,而HistoryItems设置者应该举起活动。

答案 1 :(得分:2)

重写ViewModel.HistoryItems的setter,这样就不用这样了

_historyItems = value;

它做到了

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

答案 2 :(得分:1)

NotifyPropertyChanged

的设置工具中App.ViewModel需要HistoryItems
相关问题