wpf datagrid icollectionview排序BUG?
也许有人可以帮助我?我有以下场景:
简单视图:
<按钮单击=“Button_Click”>刷新 背后的代码:
公共分部类 MainWindow : 窗口 { 公共主窗口() { 初始化组件(); DataContext = new ViewModel(); } 公共类测试项目 { 私有 int _sequence; 公共整型序列 { 获取{返回_序列; } } 公共测试项目(int序列) { _序列=序列; } } 公共类视图模型 { ObservableCollection
; _收藏; 私有 ICollectionView _view; 公共 ICollectionView 视图 { 获取{返回_view; } } 公共视图模型() { _collection = new ObservableCollection (); _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("序列", ListSortDirection.Ascending)); } } private void Button_Click(对象发送者, RoutedEventArgs e) { DataContext = new ViewModel(); } }
程序启动后,数据网格包含(如预期):
1
2
3
4
5
点击按钮后:
5
2
4
3
1
但我真的不明白为什么。我做错了什么还是这是一个错误?如果这是一个错误,有解决方法吗?
maybe someone can help me? I have the following scenario:
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>
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我刚刚遇到了这个错误。 (或者至少我认为这是一个错误)。
调试时,您可以看到在将 ViewModel 分配给 DataContext 后,SortDescriptions 集合被清除。
作为解决方法,我从 ViewModel 的 CTOR 中删除了 SortDescriptions,并将它们放入一个公共方法中,然后在将 ViewModel 分配给 DataContext 后调用该方法。
这远非理想,但这似乎是我能找到的唯一解决方法。
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.
It is far from ideal, however this seems to be the only workaround I could find.
尝试调用。
添加 SortDescription 后
Try calling
after adding the SortDescription.
您的 TestItem 未实现 IComparable 接口,因此不确定通过什么来比较您的对象。
MSDN IComparable
基本上,您需要将其添加到下面的类中。
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.