2015-09-06 64 views
0

我的ListView顯示數據庫表。問題是,即使在刷新之後,它也不會顯示對錶格的任何更改。Table <> as ItemsSourceViewView-清單不刷新

public class ProfilesSourceSQLite : IProfilesSource 
{ 
    public event EventHandler<ProfileActionEventArgs> ActionPerformed; 

    private Table<ProfileManager.Profile> Profiles; 
    public IEnumerable<ProfileManager.Profile> ProfilesCollection { get { return Profiles; } } 

    public ProfilesSourceSQLite() 
    { 
     ... 
     dbContext = new DataContext(dbConnection); 
     Profiles = dbContext.GetTable<ProfileManager.Profile>(); 
    } 

    public bool AddProfile(ProfileManager.Profile profile) 
    { 
     try 
     { 
      Profiles.InsertOnSubmit(profile); 
      dbContext.SubmitChanges(); 
     } 
     ... 

     if(ActionPerformed != null) 
      ActionPerformed(this, new ProfileActionEventArgs(ProfileAction.Add, profile)); 
    } 
} 

public class ProfileManager 
{ 
    private static ProfileManager instance; 
    public static ProfileManager Instance 
    { 
     get 
     { 
      if (instance == null) 
       instance = new ProfileManager(); 

      return instance; 
     } 
    } 

    private IProfilesSource profilesSource; 
    public IEnumerable<Profile> ProfilesCollection { get { return profilesSource.ProfilesCollection; } } 

    public event EventHandler ProfilesListChanged; 

    public ProfileManager() 
    { 
     ... 
     profilesSource = new ProfilesSourceSQLite(); 
     profilesSource.ActionPerformed += OnActionPerformed; 
    } 

    private void OnActionPerformed(object sender, ProfileActionEventArgs e) 
    { 
     if(ProfilesListChanged != null) 
      ProfilesListChanged(this, new EventArgs()); 
    } 

    public bool AddProfile(Profile profile) 
    { 
     return profilesSource.AddProfile(profile); 
    } 
} 

public class ProfileManagerWindow : Window 
{ 
    public ProfileManagerWindow() 
    { 
     InitializeComponent(); 

     ProfileManager.Instance.ProfilesListChanged += RefreshList; 
     listView.ItemsSource = ProfileManager.Instance.ProfilesCollection; 
    } 

    private void RefreshList(object sender, EventArgs e) 
    { 
     listView.Items.Refresh(); 

     // when I add new profile: 
     int x = ProfileManager.Instance.ProfilesCollection.Count; // x = 2 as it is supposed to be 
     int y = listView.Items.Count;        // y = 1 so the new profile isn't added to the list view 

     // I also tried: 
     listView.ItemsSource = null; 
     listView.ItemsSource = ProfileManager.Instance.ProfilesCollection; 
     // it also doesn't work 
    } 
} 

ProfileManager.ProfilesCollection正在更新,因爲列表上的不同操作都會執行。但ListView始終顯示啓動時加載的內容。它看起來像Refresh()方法什麼都不做。有什麼問題?爲什麼它不起作用?

回答

0

ProfilesCollectionIEnumerable,當它改變它也不會自動枚舉它不能通知列表:

public IEnumerable<ProfileManager.Profile> ProfilesCollection { get { return Profiles; } } 

要麼使用ToList這裏:

listView.ItemsSource = ProfileManager.Instance.ProfilesCollection.ToList(); 

或完全改變它的類型和使用ObservableCollection Class這是能夠通知聆聽者有關更改。

+0

但是,我在通知發生更改時。 'Refresh()'方法是否足以讓'ListView'重新加載'ItemsSource'中的所有項目?我試圖避免在內存中存儲兩個相同列表的副本(一個在'Table <>'中,另一個當我使用'ProfilesCollection.ToList()'時。 – micnyk

相關問題