M_K
M_K

Reputation: 3455

How to update Listbox after deletion from isolated storage

Hi I am trying to update a listbox that's bound after I delete a folder but it doesn't seem to update unless I go out of page and back can anyone help I have my code with class Project below, thanks for your help.

public partial class Page3 : PhoneApplicationPage
{
    string[] fileNames;
    string[] folderNames;
    private string selectedProject = "";


    private List<Project> projectList = new List<Project>();
    public Page3()
    {
        InitializeComponent();
        showProjects();

    }

    public void showProjects()
    {

        projectList.Clear();
        IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication();
        folderNames = isf.GetDirectoryNames("/*.*");

        foreach (var name in folderNames)
        {

            fileNames = isf.GetFileNames(name + "/*.*");
            int frameCount = 0;
            foreach (var nameCount in fileNames)
            {
                frameCount++;

            }



            projectList.Add(new Project(name,frameCount));

        }


        listBoxProjects.ItemsSource = projectList;

    }


    private void listBoxProjects_SelectionChanged(object sender, SelectionChangedEventArgs e)
    {

    }


    public void DeleteDirectory(string directoryName)
    {
        try
        {
            IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication();
            if (!string.IsNullOrEmpty(directoryName) && myIsolatedStorage.DirectoryExists(directoryName))
            {
                myIsolatedStorage.DeleteDirectory(directoryName);
            }
        }
        catch (Exception ex)
        {
            // handle the exception
        }
    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        IsolatedStorageFile myIso;
        string folder = textBoxDelete.Text;
        using (myIso = IsolatedStorageFile.GetUserStoreForApplication())
        {

            String[] fileNames = myIso.GetFileNames(folder + "/*.*");

            foreach (var name in fileNames)
            {
                MessageBox.Show(name);
                myIso.DeleteFile(folder + "/" + name);




            }




            myIso.DeleteDirectory(folder);
            showProjects();
        }




    }





}


public class Project
{
    public string ProjectName { get; set; }
    public int FileCount { get; set; }


    public Project(string pName, int fileCount)
    {
        this.ProjectName = pName;
        this.FileCount = fileCount;
    }



}

}

Upvotes: 0

Views: 410

Answers (1)

BigL
BigL

Reputation: 1631

You could try to set the listbox.Itemsource to null first and then reset it with the new collection.

But i would suggest you that you put you change your List<> items to an ObservableCollection<> and then if you change the collection the changes are automatically updated in your listbox, and try using binding much easier and cleaner solution.

Upvotes: 1

Related Questions