0

嗨,我正在尝试更新在删除文件夹后绑定的列表框,但它似乎没有更新,除非我离开页面并返回任何人都可以帮助我在下面的类项目中获得我的代码,感谢您的帮助。

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;
    }



}

}

4

1 回答 1

1

您可以尝试先将 listbox.Itemsource 设置为 null,然后使用新集合将其重置。

但是我建议您将 List<> 项目更改为 ObservableCollection<>,然后如果您更改集合,更改会自动更新到您的列表框中,并尝试使用更简单、更清洁的绑定解决方案。

于 2012-04-06T14:41:18.600 回答