Is there any way to dynamically sort data bound items in a RadListBox and keep them sorted

1 Answer 66 Views
ListBox
Timothy J
Top achievements
Rank 2
Bronze
Iron
Iron
Timothy J asked on 01 Dec 2022, 10:38 PM

We have a RadListBox bound to a ObservableCollection of models.  We would like sort them based on a property.  The property value can also change.  Is there any way to sort them?

What we are trying to do is create a "mover" to rank items, is a RadListBox not the best choice?

1 Answer, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 02 Dec 2022, 02:53 PM

Hello Timothy,

The RadListBox control itself doesn't have a built-in sorting feature, but you can use a collection view implementation like QueryableCollectionView (QCV) that provides support for this. Note that with the QueryableCollectionView an update of the underlying property value (used in the sorting), won't update the current view automatically. You should force this manually by calling the Reset method of the SortDescriptors collection of the QCV.

queryableCollectionView.SortDescriptors.Reset();

In case you need the view to get updated automatically on property changed, you can use the native ListCollectionView implementation. For example:

ListCollectionView cv = (ListCollectionView)CollectionViewSource.GetDefaultView(originalSource);
cv.IsLiveSorting = true;
cv.SortDescriptions.Add(new SortDescription("MySortingValue", ListSortDirection.Descending));
this.radListBox.ItemsSource = cv;

You can see this approach in the attached project. I hope it helps.

Regards,
Martin Ivanov
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Tags
ListBox
Asked by
Timothy J
Top achievements
Rank 2
Bronze
Iron
Iron
Answers by
Martin Ivanov
Telerik team
Share this question
or