I'm writing a sort algorithm visualizer in C#/WPF using MVVMLight and Fody. In my ViewModel I have an observable collection like this:
public ObservableCollection<int> NumbersCollection{ get; set; }
Now in a command (method) I change the contents of the collection (in this case I'm doing a bubble sort). Like this:
IMutableSortAlgorithm sorter = ServiceLocator.Current.GetInstance<IMutableSortAlgorithm>();
sorter.MuatbleSort(this.NumbersCollection);
while (!sorter.Finished())
{
sorter.NextStep();
this.RaisePropertyChanged("NumbersCollection"); // not neccesary
Thread.Sleep(400);
}
After a call of sorter.NextStep() the collection is changed. After each step I tried to update the View with calling RaisePropertyChanged and sleeping for 400ms.
So I basically want the view to update after EVERY change (step), but the View is only updated after the method is finished. (And for that I don't even need to call RaisePropertychanged...)
I could use a background worker for convenience. But I first want to solve the problem without an additional worker thread. Since I'm in the main GUI thread, there's no need for calling the Dispatcher.Invoke, right? (I tried and it didn't work anyway..).
Does somebody have a clue how to refresh the UI after each step?
EDIT: sorter.NextStep() does not insert or remove items to the list, but it swaps values. Like col[i] = col[j] ... you know - sorting stuff. I want to get that changes in realtime on the UI.