3
當ListBox項從ListBox中刪除時如何刷新UserInterface。ListBoxItem從數據庫中被刪除,但列表框沒有被刷新。當我按下後退按鈕並導航到頁面時,它將被刪除。任何人都可以告訴我如何刷新列表框。我認爲這是由於可觀察的收集,我不知道如何實現NotifyProperty已更改爲此代碼。任何人都可以幫助我嗎?當listBoxItem在c中被刪除時ListBox不刷新#
C#代碼中插入,讀取和數據庫
刪除項目
public class DatabaseHelperClass
{
SQLiteConnection dbConn;
//Create Tabble
public async Task<bool> onCreate(string DB_PATH)
{
try
{
if (!CheckFileExists(DB_PATH).Result)
{
using (dbConn = new SQLiteConnection(DB_PATH))
{
dbConn.CreateTable<Data>();
}
}
return true;
}
catch
{
return false;
}
}
private async Task<bool> CheckFileExists(string fileName)
{
try
{
var store = await Windows.Storage.ApplicationData.Current.LocalFolder.GetFileAsync(fileName);
return true;
}
catch (NullReferenceException exp)
{
return false;
}
catch (Exception ex)
{
return false;
}
}
// Retrieve the specific contact from the database.
public Data ReadContact(int contactid)
{
using (var dbConn = new SQLiteConnection(App.DB_PATH))
{
var existingconact = dbConn.Query<Data>("select * from Contacts where Id =" + contactid).FirstOrDefault();
return existingconact;
}
}
// Retrieve the all contact list from the database.
public ObservableCollection<Data> ReadContacts()
{
using (var dbConn = new SQLiteConnection(App.DB_PATH))
{
List<Data> myCollection = dbConn.Table<Data>().ToList<Data>();
ObservableCollection<Data> ContactsList = new ObservableCollection<Data>(myCollection);
return ContactsList;
}
}
// Insert the new contact in the Contacts table.
public void Insert(Data newcontact)
{
using (var dbConn = new SQLiteConnection(App.DB_PATH))
{
dbConn.RunInTransaction(() =>
{
dbConn.Insert(newcontact);
});
}
}
//Delete specific contact
public void DeleteContact(int Id)
{
using (var dbConn = new SQLiteConnection(App.DB_PATH))
{
var existingconact = dbConn.Query<Data>("select * from Data where Id =" + Id).FirstOrDefault();
if (existingconact != null)
{
dbConn.RunInTransaction(() =>
{
dbConn.Delete(existingconact);
});
}
}
}
//Delete all contactlist or delete Contacts table
public void DeleteAllContact()
{
using (var dbConn = new SQLiteConnection(App.DB_PATH))
{
//dbConn.RunInTransaction(() =>
// {
dbConn.DropTable<Data>();
dbConn.CreateTable<Data>();
dbConn.Dispose();
dbConn.Close();
//});
}
}
}
代碼從列表中刪除項。
public partial class Panaroma : Page
{
ObservableCollection<Data> DB_ContactList = new ObservableCollection<Data>();
public Panaroma()
{
this.InitializeComponent();
if(listBoxobj.Items.Count == 0)
{
Btn_Delete.IsEnabled = false;
}
this.Loaded += Panaroma_Loaded;
}
private void Panaroma_Loaded(object sender, RoutedEventArgs e)
{
ReadAllData dbcontacts = new ReadAllData();
DB_ContactList = dbcontacts.GetAllContacts();//Get all DB contacts
if (DB_ContactList.Count > 0)
{
Btn_Delete.IsEnabled = true;
}
listBoxobj.ItemsSource = DB_ContactList.OrderByDescending(i => i.Id).ToList();//Binding DB data to LISTBOX and Latest contact ID can Display first.
}
private void listBoxobj_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
int SelectedContactID = 0;
if (listBoxobj.SelectedIndex != -1)
{
Data listitem = listBoxobj.SelectedItem as Data;//Get slected listbox item
DatabaseHelperClass Db_Helper = new DatabaseHelperClass();
Db_Helper.DeleteContact(listitem.Id);//Delete selected DB contact Id.
}
}
你只從數據庫中刪除選定的項目。你也應該從集合中刪除它並刷新集合。對於INotifyPropertyChanged接口的實現,這裏有一些很好的例子。 – Ben
嗨@本,如何從集合中刪除選定的項目? – Rachel