I have a button, clicking it will processing all files. I want to display the progress when running it. So if file 1 is processing, then the UI displays
"processing file 1"
; when processing file 2, the UI displays
"processing file 1"
"processing file 2"
So I use a Listbox to do it. The ItemSource of the ListBox is a collection in my ViewModel
private ObservableCollection<string> _displayedFiles;
public ObservableCollection<string> DisplayedFiles
{
get {return _displayedFiles;}
set
{
_displayedFiles = value;
PropertyChanged(nameof(DisplayedFiles));
}
}
Now the ViewModel is passed to the command class
public class MyCommand :ICommand
{
private MyViewModel myViewModel;
public MyCommand(MyViewModel myViewModel)
{
this.myViewModel = myViewModel;
}
public void Execute(object parameter)
{
foreach(var f in files)
{
Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, (ThreadStart)delegate () { AddFiles(f);});
}
}
private void AddFiles(string f)
{
this.ViewModel.DisplayedFiles.Add(f);
}
}
However I found the UI is freeze and the list is not displayed one by one. It displays the whole bunch list together after the loop iteration completed.