weilitao
Topic Author
Posts: 22
Joined: 23 Feb 2016, 10:43

What's the best way to filter/sort ObservableCollection?

30 Nov 2016, 10:45

Hi,

I'd like to do filter and sorting on ObservableCollection. When try to follow some example for WPF. I have found that
CollectionViewSource.GetDefaultView
and some other methods is not exists in NoesisGUI. So what is the best way to do filter and sorting on ObservableCollection on NoesisGUI?
 
davebac
Posts: 19
Joined: 09 May 2014, 03:21

Re: What's the best way to filter/sort ObservableCollection?

01 Dec 2016, 18:42

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;
}
 
User avatar
sfernandez
Site Admin
Posts: 1652
Joined: 22 Dec 2011, 19:20

Re: What's the best way to filter/sort ObservableCollection?

01 Dec 2016, 19:38

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.
 
weilitao
Topic Author
Posts: 22
Joined: 23 Feb 2016, 10:43

Re: What's the best way to filter/sort ObservableCollection?

02 Dec 2016, 04:22

@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?
 
davebac
Posts: 19
Joined: 09 May 2014, 03:21

Re: What's the best way to filter/sort ObservableCollection?

02 Dec 2016, 21:55

@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?
I cheated.

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.
 
andekande
Posts: 5
Joined: 23 Jul 2017, 23:33

Re: What's the best way to filter/sort ObservableCollection?

25 Jul 2017, 00:10

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
 
User avatar
jsantos
Site Admin
Posts: 2584
Joined: 20 Jan 2012, 17:18
Contact:

Re: What's the best way to filter/sort ObservableCollection?

25 Jul 2017, 13:22

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.
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.
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.
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.

Who is online

Users browsing this forum: No registered users and 1 guest