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 1021097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:15:36+00:00 2026-05-16T11:15:36+00:00

I saw ICollectionView being introduced with WPF to handle situations when you need sorting

  • 0

I saw ICollectionView being introduced with WPF to handle situations when you need sorting and filtering enabled. I even saw few articles which sort items, but my main concern is why my approach is failing. Lets see my code :

 <ListView ItemsSource="{Binding}" x:Name="lvItems" GridViewColumnHeader.Click="ListView_Click">
            <ListView.View>
                <GridView AllowsColumnReorder="True">
                    <GridViewColumn Header="Id" DisplayMemberBinding="{Binding Id}" />
                    <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
                    <GridViewColumn Header="Developer">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Path=Developer}" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                    <GridViewColumn Header="Salary">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Path=Salary}" />
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>

                </GridView>
            </ListView.View>
        </ListView>

In codebehind, when the Item is clicked I am doing like this :

  ICollectionView Source { get; set; }


        private void ListView_Click(object sender, RoutedEventArgs e)
        {
            GridViewColumnHeader currentHeader = e.OriginalSource as GridViewColumnHeader;
            if(currentHeader != null && currentHeader.Role != GridViewColumnHeaderRole.Padding)
            {
                //using (this.Source.DeferRefresh())
                //{
                    SortDescription currentPropertySort = this.Source.SortDescriptions.FirstOrDefault<SortDescription>(item => item.PropertyName.Equals(currentHeader.Column.Header.ToString()));
                    if (currentPropertySort != null)
                    {
                        if (currentPropertySort.Direction == ListSortDirection.Ascending)
                            currentPropertySort.Direction = ListSortDirection.Descending;
                        else
                            currentPropertySort.Direction = ListSortDirection.Ascending;

                    }
                    else
                        this.Source.SortDescriptions.Add(new SortDescription(currentHeader.Column.Header.ToString(), ListSortDirection.Ascending));


                //}
                    this.Source.Refresh();
                    this.lvItems.DataContext = this.Source;
                    this.lvItems.UpdateLayout();
            }


        }

So whenever the header for the ListBox is clicked, the item need to be sorted. I am holding the collection using a property called Source and then using it by calling lvItems.DataContext = this.Source. But the code does not seem to be working.

  • 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-16T11:15:37+00:00Added an answer on May 16, 2026 at 11:15 am

    Here’s an updated version of your ListView_Click method that somewhat works. I’m not sure exactly what sorting behavior you were looking for but the version below “stacks up” a set of sort descriptions, making the last clicked column as the “primary sort description”. I hope this makes sense and I hope the code below helps. =)

    private void ListView_Click(object sender, RoutedEventArgs e)
    {
        GridViewColumnHeader currentHeader = e.OriginalSource as GridViewColumnHeader;
        if(currentHeader != null && currentHeader.Role != GridViewColumnHeaderRole.Padding)
        {
            if (this.Source.SortDescriptions
                .Count((item) => item.PropertyName.Equals(currentHeader.Column.Header.ToString())) > 0)                
            {
                SortDescription currentPropertySort = this.Source
                    .SortDescriptions
                    .First<SortDescription>(item => item.PropertyName.Equals(currentHeader.Column.Header.ToString()));
    
                //Toggle sort direction.
                ListSortDirection direction = 
                    (currentPropertySort.Direction == ListSortDirection.Ascending)?
                    ListSortDirection.Descending : ListSortDirection.Ascending;
    
                //Remove existing sort
                this.Source.SortDescriptions.Remove(currentPropertySort);
                this.Source.SortDescriptions.Insert(0, new SortDescription(currentHeader.Column.Header.ToString(), direction));
            }
            else
            {
                this.Source.SortDescriptions.Insert(0, new SortDescription(currentHeader.Column.Header.ToString(), ListSortDirection.Ascending));
            }
    
            this.Source.Refresh();
        }
    }
    

    EDIT:

    By the way, one of the problems in your code above is your call to “FirstOrDefault” to query an existing SortDescription. See, SortDescription is a struct, which is non-nullable so the call to FirstOrDefault will never be null and will always return an instance. Therefore, the “else-statement” in your code above will never get called.

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

Sidebar

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.