Я новичок в WPF и, похоже, я тоже не понимаю C#.
Приведенный ниже код должен предоставлять отсортированные данные DataGrid.Переменная типа ICollectionView Тип интерфейса
Вот код, я изо всех сил, чтобы понять:
ObservableCollection<Person> PersonsCollection = new ObservableCollection<Person>();
//this one is easy: I create new collection for objects of class Person and I call it PersonsCollection
ICollectionView PersonsView = CollectionViewSource.GetDefaultView(PersonsCollection);
//this one is more complicated. Does it mean that I create new object called PersonsView which I assume that implements ICollectionView interface?
ListCollectionView personsView = PersonsView as ListCollectionView;
//this one I do not understand. Why do we need it? How can we treat PersonsView as ListCollectionView?
personsView.CustomSort = new PersonSorter();
//here we assign object of PersonSorter class to do the sorting. Fine with me.
dataGrid1.ItemsSource = personsView;
//and here we assign personsView as a ItemsSource for our DataGrid. Fine with me.
Любая помощь? Спасибо :-)
ICollectionView разрешен для использования в ItemsSource? Но это интерфейс, а не сборник, и как он работает? –
Вы не можете напрямую создать интерфейс, поэтому возвращаемый объект будет конкретным классом, который реализует 'ICollectionView', если вы запросите возвращаемый тип, вы увидите, что это' ListCollectionView'. «ICollectionView» наследует от «IEnumerable», поэтому этот тип должен реализовывать «IEnumerable».Если вы посмотрите на свойство 'ItemsSource', оно имеет тип' IEnumerable', поэтому вы можете с радостью связать с любым типом, который реализует 'ICollectionView' (поскольку это означает, что он должен реализовывать' IEnumerable'. – Chris