Is there a template reverse order in Listbox? I'm using ObservableCollec开发者_StackOverflow社区tion and I would like to avoid any extra sorting, inserting etc.
If you already have an ObservableCollection that you don't want to change I would probably go with the CollectionViewSource class. You can do it directly in XAML:
...
xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"
...
<UserControl.Resources>
<CollectionViewSource x:Key="cvs" Source="{Binding}">
<CollectionViewSource.SortDescriptions>
<scm:SortDescription PropertyName="TheProperty" Direction="Descending" />
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
</UserControl.Resources>
<ListBox ItemsSource={Binding Source={StaticResource cvs}}" />
If you use Linq, you should be able to just .OrderByDescending()? Have a look at an example here - http://msdn.microsoft.com/en-us/vcsharp/aa336756.aspx
If this is for the score list you were working on from a previous question, you should be able to just order by the score amount if it's stored as an integer, without having to create a custom comparer function. An example linq query to order your list would be something like:
var sortedItems =
from item in myObservableCollection
orderby item.Score descending
select item;
Once you have the IEnumerable result of the Linq, you can recreate an ObservableCollection and reassign to your ViewModel (Cast LINQ result to ObservableCollection)
精彩评论