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

  • SEARCH
  • Home
  • 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 8318319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:55:38+00:00 2026-06-08T21:55:38+00:00

I’m trying to sort data in datagrid, but when I click on the header

  • 0

I’m trying to sort data in datagrid, but when I click on the header of the column which has binding with the converter, nothing happens. I use MVVM pattern. Example is attached below. In the example, the grid shows column (Type) which displays type of person and therefore I use converter (class TypeValueConverter). When I use this converter, the grid doesn’t sort column Type.

<Window x:Class="GridSort.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:my="clr-namespace:GridSort"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <DataGrid ItemsSource="{Binding People}" AutoGenerateColumns="False">
            <DataGrid.Resources>
                <my:TypeValueConverter x:Key="typeConverter" />
            </DataGrid.Resources>
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding FirstName}" Header="FirstName" />
                <DataGridTextColumn Binding="{Binding Surname}" Header="Surname" />
                <DataGridTextColumn Binding="{Binding Converter={StaticResource ResourceKey=typeConverter}}" Header="Type" />
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>
public class ViewModel
{
    private ICollection<Person> people;
    public ICollection<Person> People
    {
        get
        {
            if (this.people == null)
            {
                this.people = new List<Person>();
                this.people.Add(new Student() { FirstName = "Charles", Surname = "Simons" });
                this.people.Add(new Student() { FirstName = "Jake", Surname = "Baron" });
                this.people.Add(new Teacher() { FirstName = "John", Surname = "Jackson" });
                this.people.Add(new Student() { FirstName = "Patricia", Surname = "Phillips" });
                this.people.Add(new Student() { FirstName = "Martin", Surname = "Weber" });
            }

            return this.people;
        }
    }
}

public class TypeValueConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null)
            {
                return DependencyProperty.UnsetValue;
            }

            return value.GetType().Name;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

public abstract class Person
    {
        public string FirstName
        {
            get;
            set;
        }

        public string Surname
        {
            get;
            set;
        }
    }

    public class Student : Person
    {
    }

    public class Teacher : Person
    {
    }
  • 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-06-08T21:55:40+00:00Added an answer on June 8, 2026 at 9:55 pm

    I resolved this issue myself 🙂

    I created new behavior for grid with attached property UseBindingToSort. When I set this property to true then event Sorting on grid is subscribed. After grid fires event sorting I use custom comparer with IValueConverter which is defined in binding.
    Solution is below:

    Change on view

    <DataGrid ItemsSource="{Binding People}" AutoGenerateColumns="False"  my:GridSortingBehavior.UseBindingToSort="True">
    

    New behavior with attached property:

    public static class GridSortingBehavior
        {
            public static readonly DependencyProperty UseBindingToSortProperty = DependencyProperty.RegisterAttached("UseBindingToSort", typeof(bool), typeof(GridSortingBehavior), new PropertyMetadata(new PropertyChangedCallback(GridSortPropertyChanged)));
    
            public static void SetUseBindingToSort(DependencyObject element, bool value)
            {
                element.SetValue(UseBindingToSortProperty, value);
            }
    
            private static void GridSortPropertyChanged(DependencyObject elem, DependencyPropertyChangedEventArgs e)
            {
                DataGrid grid = elem as DataGrid;
                if (grid != null){
                    if ((bool)e.NewValue)
                    {
                        grid.Sorting += new DataGridSortingEventHandler(grid_Sorting);
                    }
                    else
                    {
                        grid.Sorting -= new DataGridSortingEventHandler(grid_Sorting);
                    }
                }
            }
    
            static void grid_Sorting(object sender, DataGridSortingEventArgs e)
            {
                DataGridTextColumn clm = e.Column as DataGridTextColumn;
                if (clm != null)
                {
                    DataGrid grid = ((DataGrid)sender);
                    IValueConverter converter = null;
                    if (clm.Binding != null)
                    {
                        Binding binding = clm.Binding as Binding;
                        if (binding.Converter != null)
                        {
                            converter = binding.Converter;
                        }
                    }
    
                    if (converter != null)
                    {
                        e.Handled = true;
                        ListSortDirection direction = (clm.SortDirection != ListSortDirection.Ascending) ? ListSortDirection.Ascending : ListSortDirection.Descending;
                        clm.SortDirection = direction;
                        ListCollectionView lcv = (ListCollectionView)CollectionViewSource.GetDefaultView(grid.ItemsSource);
                        lcv.CustomSort = new ComparerWithComparer(converter, direction);
                    }
                }
            }
    

    And finally my custom comparer:

    class ComparerWithComparer : IComparer
        {
            private System.Windows.Data.IValueConverter converter;
            private System.ComponentModel.ListSortDirection direction;
    
    
            public ComparerWithComparer(System.Windows.Data.IValueConverter converter, System.ComponentModel.ListSortDirection direction)
            {
                this.converter = converter;
                this.direction = direction;
            }
    
            public int Compare(object x, object y)
            {
                object transx = this.converter.Convert(x, typeof(string), null, System.Threading.Thread.CurrentThread.CurrentCulture);
                object transy = this.converter.Convert(y, typeof(string), null, System.Threading.Thread.CurrentThread.CurrentCulture);
                if (direction== System.ComponentModel.ListSortDirection.Ascending){
                    return Comparer.Default.Compare(transx, transy);
                }
                else
                {
                    return Comparer.Default.Compare(transx, transy) * (-1);
                }
            }
        }
    

    And that’s all.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I want to construct a data frame in an Rcpp function, but when I
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace

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.