在c#中删除listBoxItem时ListBox不刷新
Posted
技术标签:
【中文标题】在c#中删除listBoxItem时ListBox不刷新【英文标题】:ListBox doesn't refresh when listBoxItem is deleted in c# 【发布时间】:2018-01-09 17:56:43 【问题描述】:从 ListBox 中删除列表框项时如何刷新 UserInterface。ListBoxItem 正在从数据库中删除,但列表框未刷新。当我按下后退按钮并导航到它被删除的页面时。谁能告诉我如何刷新列表框。我认为这是由于可观察的集合,我不知道如何实现此代码更改的 NotifyProperty。有人可以帮我吗?
用于从数据库中插入、读取和删除项目的 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,如何从集合中删除所选项目? 【参考方案1】:要从集合中删除项目,请使用方法Remove
(listitem) msdn 并更新相关的列表框。
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
【讨论】:
以上是关于在c#中删除listBoxItem时ListBox不刷新的主要内容,如果未能解决你的问题,请参考以下文章
如何知道一个显示的listboxItem是否在WPF ListBox中?
WPF Listbox 控件模板不显示 Listboxitem 控件模板和 ItemTemplate 数据模板
自定义的 ListBoxItem 自适应ListBox的宽度