wpf datagrid icollectionview排序BUG? [英] wpf datagrid icollectionview sorting BUG?
问题描述
-
一个简单的视图:
< Window x:Class =DataGridSortBug.MainWindow
xmlns =http://schemas.microsoft.com/winfx/2006/xaml/presentation
xmlns:x =http://schemas.microsoft.com/winfx/2006/xaml
Title =MainWindowHeight =350Width =525>
& DockPanel>
< StackPanel DockPanel.Dock =Top>
< Button Click =Button_Click>刷新< / Button>
< / StackPanel>
< DataGrid ItemsSource ={Binding View}/>
< / DockPanel>
< / Window>
-
代码背后:
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> _采集;
私人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 );
}
}
程序启动后,datagrid包含(如预期):
1
2
3
4
5
点击按钮:
5
2
4
3,
1
但是我真的不明白为什么。我做错了,还是这个bug?如果这是一个错误,是否有解决方法?
我刚遇到这个bug。 (或至少我认为它是一个错误)。
调试时,可以看到在将ViewModel分配给DataContext之后,SortDescriptions集合被清除。 >
作为一个工作,我从ViewModel的CTOR中删除了SortDescriptions,并将它们放在一个公共方法中,然后在将ViewModel分配给DataContext之后调用它。
private void Button_Click(object sender,RoutedEventArgs e)
{
var model = new ViewModel();
DataContext = model; // SortDescriptions集合在这里被清除。
model.AddSortDescriptions();
model.View.Refresh();
}
这是远非理想的,但这似乎是唯一的解决方法找到。
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?
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.
这篇关于wpf datagrid icollectionview排序BUG?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!