I am creating a control that displays a summary of data. On this page I am binding a control to an ObservableColection that contains many items. I created a CollectionView that sorts the items by time but in this control I only want to show the top 3 items. I tried creating a converter to accomplish this but all ways I try to filter the list return a “new” list and the control is no longer updated when a new item is added to the master ObservableCollection.
Is there a clean way to accomplish this? I will have to do something similar for several of my collections so I would like to make something generic.
If you’re using the ViewModel pattern, the clean way is to have another ObservableCollection with the top N items. That way the binding is direct and you can change 3 to whatever N you want only by changing your model.
If you want to that care of it only through XAML (no the cleaneast way):
This example shows a ListBox but you can use it on any other control.