2011-10-25 13 views
0

私はバインドするリストボックスを持っていますHistoryItemsHistoryItemsObservableCollectionHistoryです。ここ は、リストボックスの宣言である:ここで私のリストボックスは更新されません

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

リストが読み込まれますが、私はいくつかのピボットの子供たちにいくつかのmodifsを行った後、と行きますバックメインパノラマビューに、私はOnNavigatedToHistoryItemsを更新しよう:

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

が、LIS tboxは更新されず、関数は正しいデータを返します。何が問題なの? HistoryINotifyPropertyChangedであり、HistoryItemsObservableCollection<History>なので、問題はありません。リストを更新しない原因は何ですか?

答えて

4

HistoryItemsは、リフレッシュ時に置き換えられるので、それはObservableCollectionである必要はありません。

HistoryItemsをクリアしてから、新しいアイテムを追加することができます。またはViewModelINotifyPropertyChangedを実装し、HistoryItems設定者はイベントを発生させる必要があります。

1

あなたが代わりにこの

_historyItems = value; 

を行うので、それはそう、この

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

ないようにViewModel.HistoryItems用セッターを書き換え

HistoryItems

2
のセッターに App.ViewModelため NotifyPropertyChangedを必要としています。バインディングはObservableCollection によって直接サポートされているため、それが観測可能とラベル付けされています。
関連する問題