在c#中删除listBoxItem时,ListBox不会刷新

时间:2017-08-02 13:17:19

标签: c# xaml listbox windows-phone-8.1 observablecollection

如何从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.
        }
    }

1 个答案:

答案 0 :(得分:2)

要从Collection中删除项目,请使用方法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
        //...
    }
}

要更新ListBox,请查看此post