wpf datagrid icollectionview sorting BUG?
Asked Answered
P

3

3

maybe someone can help me? I have the following scenario:

  1. A simple view:

    <Window x:Class="DataGridSortBug.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="MainWindow" Height="350" Width="525">
        <DockPanel>
            <StackPanel DockPanel.Dock="Top">
                <Button Click="Button_Click">Refresh</Button>
            </StackPanel>
    
            <DataGrid ItemsSource="{Binding View}" />
       </DockPanel>
    </Window>
    
  2. The code behind:

    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
    
            DataContext = new ViewModel();
        }
    
        public class TestItem
        {
            private int _sequence;
            public int Sequence
            {
                get { return _sequence; }
            }
    
            public TestItem(int sequence)
            {
                _sequence = sequence;
            }
        }
    
        public class ViewModel
        {
            ObservableCollection<TestItem> _collection;
    
            private ICollectionView _view;
            public ICollectionView View
            {
                get { return _view; }
            }
    
            public ViewModel()
            {
                _collection = new ObservableCollection<TestItem>();
                _collection.Add(new TestItem(5));
                _collection.Add(new TestItem(2));
                _collection.Add(new TestItem(4));
                _collection.Add(new TestItem(3));
                _collection.Add(new TestItem(1));
    
                _view = CollectionViewSource.GetDefaultView(_collection);
                _view.SortDescriptions.Add(new SortDescription("Sequence", ListSortDirection.Ascending));
            }
    
    
        }
    
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            DataContext = new ViewModel();
        }
    }
    

After the program startup the datagrid contains (as expected):

1
2
3
4
5

After click on the button:

5
2
4
3
1

But I really can't understand why. Am I doing something wrong or is this a bug? And if this is a bug is there a workaround?

Persimmon answered 30/5, 2011 at 13:15 Comment(2)
connect.microsoft.com/WPF/feedback/details/671505/…Messner
A rather old thread... but it's never to late to add a solution/workaround, is it? Well, I've run into the same problem and here's my solution: #11177851Chukchi
E
1

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.

Erratum answered 25/8, 2011 at 9:10 Comment(0)
H
0

Try calling

_view.Refresh(); 

after adding the SortDescription.

Hardcastle answered 23/8, 2011 at 3:36 Comment(0)
R
-1

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
    }
Rosaline answered 23/8, 2011 at 3:26 Comment(1)
The OP states that the sorting worked the first time the view is generated, so this doesn't make sense.Quiz

© 2022 - 2024 — McMap. All rights reserved.