Search code examples
wpfxamlobservablecollectioncollectionviewsource

How to set condition instead of PropertyName in the SortDescription while sorting a CollectionViewSource?


I want to sort a CollectionViewSource but i need to set a condition instead of PropertyName.In Fact i want to do the sort below using CollectionViewSource in Xaml.

  Class2Colection.OrderBy(s => s.Id).OrderBy(s =>!s.Id.HasValue));

  <CollectionViewSource Source="{Binding Class2Colection}">
       <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="Id" Direction="Ascending"/>
            <scm:SortDescription PropertyName="??" Direction="Ascending"/>
       </CollectionViewSource.SortDescriptions>
  </CollectionViewSource>

Solution

  • You can apply custom sorting logic by implementing your own IComparer and setting it to public CustomSort property of ListCollectionView class. But this is not complete answer to your question and rather a direction where you can move forward.

    The simplest way you can achieve just this

    Class2Colection.OrderBy(s => s.Id).OrderBy(s =>!s.Id.HasValue));
    

    in xaml is to expose another property in your data class which whould return !Id.HasValue and sort by it.