I've a ListBox
fed by a view model property (Photos
) which is an ObservableCollection
of objects containing paths to image files. The ListBox
displays the images, which can be numerous:
View:
<ListBox ItemsSource="{Binding Path=Photos}"
SelectionChanged="PhotoBox_SelectionChanged">
...
</ListBox>
Code behind (which could be improved to run asynchronously...):
void RefreshPhotoCollection (string path) {
Photos.Clear ();
var info = new DirectoryInfo (path);
try {
foreach (var fileInfo in info.EnumerateFiles ()) {
if (FileFilters.Contains (fileInfo.Extension)) {
Photos.Add (new Photo (fileInfo.FullName));
}
}
}
catch (Exception ex) when (ex is IOException || ex is UnauthorizedAccessException) {
...
}
}
I've managed to display a wait cursor when running RefreshPhotoCollection
by using this method which involves a IDisposable:
using (this.BusyStackLifeTracker.GetNewLifeTracker())
{
// long job
}
But the cursor is reset to a pointer at the end of this method, when the view is notified of the collection change. The ListBox
then renders itself, but the wait cursor is not displayed while this is done. There is were I have a problem.
How can I manage to have the ListBox
displaying a wait cursor until the update is complete?