[英]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.您好,我正在尝试更新删除文件夹后绑定的列表框,但它似乎不会更新,除非我 go 页外和返回任何人都可以帮助我在下面的 class 项目中获取我的代码,感谢您的帮助。
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;
}
}
} }
You could try to set the listbox.Itemsource to null first and then reset it with the new collection.您可以尝试先将 listbox.Itemsource 设置为 null,然后使用新集合将其重置。
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.但我建议您将您的 List<> 项目更改为 ObservableCollection<>,然后如果您更改集合,更改会自动更新到您的列表框中,并尝试使用绑定更简单和更清洁的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.