3

也许有人可以帮助我?我有以下情况:

  1. 一个简单的看法:

    <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. 后面的代码:

    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();
        }
    }
    

程序启动后,数据网格包含(如预期的那样):

1
2
3
4
5

点击按钮后:

5
2
4
3
1

但我真的不明白为什么。我做错了什么还是这是一个错误?如果这是一个错误,是否有解决方法?

4

3 回答 3

1

我刚刚遇到了这个错误。(或者至少我认为这是一个错误)。

调试时,您可以看到将 ViewModel 分配给 DataContext 后,SortDescriptions 集合被清除。

作为一种解决方法,我从 ViewModel 的 CTOR 中删除了 SortDescriptions,并将它们放在一个公共方法中,然后在将 ViewModel 分配给 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();
}

这远非理想,但这似乎是我能找到的唯一解决方法。

于 2011-08-25T09:10:53.097 回答
0

试着打电话

_view.Refresh(); 

添加排序描述后。

于 2011-08-23T03:36:39.953 回答
-1

您的 TestItem 没有实现 IComparable 接口,因此它不确定通过什么来比较您的对象。

MSDN IComparable

基本上,您需要将其添加到下面的课程中。

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
    }
于 2011-08-23T03:26:46.083 回答