wpf datagrid icollectionview sorting BUG?

强颜欢笑 提交于 2019-12-05 18:39:14

I just ran into this bug. (Or at least I presume it is a bug).

When debugging, you can see that the SortDescriptions collection gets cleared after assigning the ViewModel to the DataContext.

As a work around, I removed the SortDescriptions from the CTOR of the ViewModel and put them within a public method which I then call after assigning the ViewModel to the DataContext.

private void Button_Click(object sender, RoutedEventArgs e)
{
    var model = new ViewModel();
    DataContext = model;  // SortDescriptions collection is cleared here.
    model.AddSortDescriptions();
    model.View.Refresh();
}

It is far from ideal, however this seems to be the only workaround I could find.

Try calling

_view.Refresh(); 

after adding the SortDescription.

Your TestItem is not implementing the IComparable interface so it is not sure of what to compare your objects by.

MSDN IComparable

Basically you need to add this to your class below.

public class TestItem  : IComparable
{
    private int _sequence;
    public int Sequence
    {
        get { return _sequence; }
    }

    public TestItem(int sequence)
    {
        _sequence = sequence;
    }

   public int CompareTo(object obj) 
   {
     if (obj == null) 
          return 1;
     // put comparison logic here
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!