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()
方法什麼都不做。有什麼問題?爲什麼它不起作用?
但是,我在通知發生更改時。 'Refresh()'方法是否足以讓'ListView'重新加載'ItemsSource'中的所有項目?我試圖避免在內存中存儲兩個相同列表的副本(一個在'Table <>'中,另一個當我使用'ProfilesCollection.ToList()'時。 – micnyk