Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6242537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T11:58:22+00:00 2026-05-24T11:58:22+00:00

I am working with WPF and encountered a problem about sorting in a List.

  • 0

I am working with WPF and encountered a problem about sorting in a List.
I find the sample that it use OrderBy( T => T.[the field that refer to it for sorting])
e.g.

List<Contact> contacts = new List<Contact>();
contacts.OrderBy(Contact => Contact.PhoneNumber)

It works perfectly.

However, if I don’t know the type and even don’t know the field in that type, how should I implement the sorting function?

Like: what should I put in in OrderBy(??? => ????.?????)

In fact, I wanna make a general GridView in which, when user click on one of the header, it will sort the whole list by the corresponding column.

Thank you so much!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T11:58:23+00:00Added an answer on May 24, 2026 at 11:58 am

    Two solutions for dynamic sorting …

    1. GridView’s ItemsSource is set to a ICollectionView or ListCollectionView with SourceCollection set as your own List and having a SortDescription where PropertyName can be specified as a string value… http://marlongrech.wordpress.com/2008/11/22/icollectionview-explained/

    2. Dynamic LINQ with MyList.AsQueryable().OrderBy(<OrderByClause>) where order by clause can be created dynamically… How do I apply OrderBy on an IQueryable using a string column name within a generic extension method?

    So when user clicks on the grid view column headers, you have to extract the property name that the column is representing and use any of the two above approaches. In option 2 you would have to set the ItemsSource after the collection is sorted.

    Any of the two approaches can be done in an Attached Behavior that works on GridViews in order to have a generic solution.

    <ListView myNameSpace:ListSortingBehavior.SortingSource="CollectionView" ... /> <!-- CollectionView SortDescriptions -->
    

    OR

    <ListView myNameSpace:ListSortingBehavior.SortingSource="Generic" ... /> <!-- AsQueryable LINQ -->
    

    All you have to worry about it if you use option1, then you would have to assume that the GridView.ItemsSource is always going to be an ICollectionView. In second approach the trade off is you would have to “set” the ItemsSource after sorting.

    I hope this helps…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got style resources in WPF working so that in my App.xaml I can
I'm working on a WPF application that sometimes exhibits odd problems and appears to
I am working on a WPF application which has a treeview that represents an
I'm working on a WPF app that needs to host some Flash content. The
I am working in WPF application with excel . In that application i am
We have a working WPF app that we are looking into running in the
I have a working WPF dialog that uses DataGrid. The DataGrid is set to
I am working with WPF and c#. I have a list of objects. Example:
I've encountered a problem when converting a WPF project from vs2008 to vs2010. I
I am currently working on a WPF application that processes data in a database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.