Im trying to develop a race track scoreboard like this:
The problem is that I dont know which is the best practice to do it. Actually I tryied to create an Observable Collection that is updated constantly. Well, the problem is that when I try to sort the scoreboard dinamically by drivers best lap, the drivers position is always static.
I used a CollectionViewSource combined with a ListBox for sort the drivers by the Property "BestLap" but It seems that the drivers position is sorted only when I run the program for the first time, then don't sort anything.
I also tryied to sort the ObservableCollection constantly in the viewmodel, making the Driver class IComparable and creating a new ObservableCollection that sorts comparing drivers by bestlap. But it think that's not a good practice.
I would like to find a sample that does something similar but I didn't find it yet. Please let me know if you have any suggestions about how to do this.
Sorry for my english.
Thank you very much!
Using an ObservableCollection (OC) of e.g. drivers is the correct approach. Furthermore using an CollectionViewSource (CVS) is a good way, too. The resulting problem in your case is, that your CVS just gets actualised when the Source (the OC) changes. This means if a driver gets added or removed.
What you want is to be notified when a property (like "BestLap") of an object of your Source changes.
There are several questions/answers on stackoverflow and other sites dealing with this problem.
Now to a possible solution (extracted from the second link): Enable "IsLiveSortingRequested" and add a "SortDescription" containing the property being utilised for sorting.
EDIT:
Here is a (very simple and basic) working example using a proper MVVM approach:
Model (driver.cs):
The ViewModel.cs:
The View (MainWindow.xaml):
And finally the MainWindow.cs