2012-04-06 75 views
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; 
    } 



} 

}

回答

1

你可以嘗試設置listbox.Itemsource先空,然後用新的集合重置。

但是我建議你把你的List項目更改爲ObservableCollection <>然後如果你改變了集合,這些更改會自動更新到你的列表框中,並嘗試使用綁定更容易和更清晰的解決方案。

+0

ObservableCollection工作很奇怪我通過我的項目使用它,然後只是決定使用這個列表,謝謝 – 2012-04-06 15:45:47

+0

@Steven_M有時你也會遇到我,我只是開始使用List而不是ObservableCollection :) – BigL 2012-04-06 20:25:36