在 c# 中删除 listBoxItem 时 ListBox 不刷新
ListBox doesn't refresh when listBoxItem is deleted in c#
从 ListBox.The 中删除列表框项时如何刷新用户界面 ListBox.The ListBoxItem 正在从数据库中删除,但列表框未刷新。当我按下后退按钮并导航到页面时 deleted.Can 有人告诉我如何刷新列表框。我认为这是由于可观察集合,我不知道如何实现为此更改的 NotifyProperty code.Can 有人帮我吗?
C# code to insert,read and delete items from database
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();
//});
}
}
}
Code to delete item from list.
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.
}
}
要从集合中删除项目,请使用方法 Remove
(listitem) msdn 并更新相关的 ListBox。
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.
//remove item from collection
DB_ContactList.Remove(listitem);
//update listbox
//...
}
}
要更新列表框,请查看此 post
从 ListBox.The 中删除列表框项时如何刷新用户界面 ListBox.The ListBoxItem 正在从数据库中删除,但列表框未刷新。当我按下后退按钮并导航到页面时 deleted.Can 有人告诉我如何刷新列表框。我认为这是由于可观察集合,我不知道如何实现为此更改的 NotifyProperty code.Can 有人帮我吗?
C# code to insert,read and delete items from database
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();
//});
}
}
}
Code to delete item from list.
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.
}
}
要从集合中删除项目,请使用方法 Remove
(listitem) msdn 并更新相关的 ListBox。
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.
//remove item from collection
DB_ContactList.Remove(listitem);
//update listbox
//...
}
}
要更新列表框,请查看此 post