What's the best way to filter/sort ObservableCollection?
Hi,
I'd like to do filter and sorting on ObservableCollection. When try to follow some example for WPF. I have found that and some other methods is not exists in NoesisGUI. So what is the best way to do filter and sorting on ObservableCollection on NoesisGUI?
I'd like to do filter and sorting on ObservableCollection. When try to follow some example for WPF. I have found that
Code: Select all
CollectionViewSource.GetDefaultView
Re: What's the best way to filter/sort ObservableCollection?
Code: Select all
public class FilteredObservable<T> : INotifyPropertyChanged
{
private ObservableCollection<T> _baseCollection;
public ObservableCollection<T> BaseCollection
{
get
{
return _baseCollection;
}
set
{
// emulate VB's "with events" properties.
if (null != _baseCollection)
_baseCollection.CollectionChanged -= _baseCollection_CollectionChanged;
_baseCollection = value;
_baseCollection.CollectionChanged += _baseCollection_CollectionChanged;
NotifyPropertyChange("BaseCollection");
NotifyPropertyChange("Items");
}
}
public IEnumerator<T> Items
{
get
{
foreach(var x in _baseCollection)
{
if (FilterFunction(x)) // replace true with whatever filter logic you need.
yield return x;
}
}
}
private void _baseCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
NotifyPropertyChange("Items");
}
public FilteredObservable(ObservableCollection<T> collection)
{
BaseCollection = collection;
FilterFunction = (e) => { return true; };
}
public FilteredObservable()
{
BaseCollection = new ObservableCollection<T>();
FilterFunction = (e) => { return true; };
}
protected void NotifyPropertyChange(string name)
{
if (null != PropertyChanged)
PropertyChanged(this, new PropertyChangedEventArgs(name));
}
public Func<T, bool> FilterFunction;
public event PropertyChangedEventHandler PropertyChanged;
}
-
sfernandez
Site Admin
- Posts: 2983
- Joined:
Re: What's the best way to filter/sort ObservableCollection?
Thanks a lot @davebac for your contribution.
Sorting and filtering is not implemented in NoesisGUI, so as davebac posted you should expose the filtered/sorted collection in your view model and bind that one to the ItemsControl instead of the original collection.
Sorting and filtering is not implemented in NoesisGUI, so as davebac posted you should expose the filtered/sorted collection in your view model and bind that one to the ItemsControl instead of the original collection.
Re: What's the best way to filter/sort ObservableCollection?
@davebac, thanks your code.
@sfernandez, thanks for your explanation.
Another question about @davebac's code. The class FilteredObservable is just INotifyPropertyChanged, but not ObservableCollection, so I need to use `FilteredObservable.Items' to bind to ItemsControl. but `Items' property is also not ObservableCollection. If I change items inside BaseCollection (the original one), how to make FilteredObservable.Items reflect the change automatically?
@sfernandez, thanks for your explanation.
Another question about @davebac's code. The class FilteredObservable is just INotifyPropertyChanged, but not ObservableCollection, so I need to use `FilteredObservable.Items' to bind to ItemsControl. but `Items' property is also not ObservableCollection. If I change items inside BaseCollection (the original one), how to make FilteredObservable.Items reflect the change automatically?
Re: What's the best way to filter/sort ObservableCollection?
I cheated.@davebac, thanks your code.
@sfernandez, thanks for your explanation.
Another question about @davebac's code. The class FilteredObservable is just INotifyPropertyChanged, but not ObservableCollection, so I need to use `FilteredObservable.Items' to bind to ItemsControl. but `Items' property is also not ObservableCollection. If I change items inside BaseCollection (the original one), how to make FilteredObservable.Items reflect the change automatically?
When the collection changes, I indicate that the Items property has been re-assigned. That'll trigger a rebind, provided that something up-chain is bound to the FilteredObservable. Or ought to anyway.
Re: What's the best way to filter/sort ObservableCollection?
Hey I want to warm up that topic since on the base of ICollectionView also Filter, Group, Sort, Add, Remove operations would be given (ListCollectionView).
There are Mono compatible implementations, so at least in Unity one could acces it. Can you help me to complete the picture. Why is it not part of Noesis and how would I be able to emulate it.
From my understanding, there should be a class CollectionViewSource provided, but the implementation for ICollectionView (what is returned from GetDefaultView) that does the actual sorting must not be implemented.
I understand it makes no sense to rebuild the .Net BaseClassLibraries. What I wonder is, how the injection mechanism would look like, if i use Noesis in a WPF app to for example render a huge DataGrid and want to bind it to a real .Net ListCollectionView.
Possibly I get it all wrong, since CollectionViewSource is also specially handled in the Binding system in WPF (actually its implicitly always created when binding to a collection). You run your own Binding system, right? However I want to learn more about those and similar extension scenarios.
Maybe this helps somehow:
https://github.com/matthewbednarski/MVV ... wSource.cs
There are Mono compatible implementations, so at least in Unity one could acces it. Can you help me to complete the picture. Why is it not part of Noesis and how would I be able to emulate it.
From my understanding, there should be a class CollectionViewSource provided, but the implementation for ICollectionView (what is returned from GetDefaultView) that does the actual sorting must not be implemented.
I understand it makes no sense to rebuild the .Net BaseClassLibraries. What I wonder is, how the injection mechanism would look like, if i use Noesis in a WPF app to for example render a huge DataGrid and want to bind it to a real .Net ListCollectionView.
Possibly I get it all wrong, since CollectionViewSource is also specially handled in the Binding system in WPF (actually its implicitly always created when binding to a collection). You run your own Binding system, right? However I want to learn more about those and similar extension scenarios.
Maybe this helps somehow:
https://github.com/matthewbednarski/MVV ... wSource.cs
Re: What's the best way to filter/sort ObservableCollection?
Well, it is not part of Noesis because we didn't have time to implement it yet. But we know this is important and we are working on it. The workaround is explained above.There are Mono compatible implementations, so at least in Unity one could acces it. Can you help me to complete the picture. Why is it not part of Noesis and how would I be able to emulate it.
Main problem with Noesis is that all the core is implemented in C++. So, all the concepts added in the C# layer need a proper implementation in the core. So, these new interfaces and base classes need to be properly understood by our C++ code.From my understanding, there should be a class CollectionViewSource provided, but the implementation for ICollectionView (what is returned from GetDefaultView) that does the actual sorting must not be implemented.
I understand it makes no sense to rebuild the .Net BaseClassLibraries. What I wonder is, how the injection mechanism would look like, if i use Noesis in a WPF app to for example render a huge DataGrid and want to bind it to a real .Net ListCollectionView.
Possibly I get it all wrong, since CollectionViewSource is also specially handled in the Binding system in WPF (actually its implicitly always created when binding to a collection). You run your own Binding system, right? However I want to learn more about those and similar extension scenarios.
Who is online
Users browsing this forum: Ahrefs [Bot], Google [Bot] and 35 guests