-->

Implementing ICollectionViewLiveShaping

2019-03-15 09:51发布

问题:

Can anybody help me properly implement ICollectionViewLiveShaping for the purpose of filtering? I haven't found much useful documentation online regarding this issue. Here's what I have:

public ICollectionView WorkersEmployed { get; set; }

WorkersEmployed = new CollectionViewSource { Source = GameContainer.Game.Workers }.View;

I'm not using GetDefaultView because I need multiple instances of filters on this collection. If it matters, GameContainer.Game.Workers is an ObservableCollection.

ApplyFilter(WorkersEmployed);

private void ApplyFilter(ICollectionView collectionView)
{
    collectionView.Filter = IsWorkerEmployed;
}

public bool IsWorkerEmployed(object item)
{
    Worker w = item as Worker;
    return w.EmployerID == this.ID;
}

This all works, but of course it must be manually refreshed, which is why I'm trying to use ICollectionViewLiveShaping. The best example I could find was this, but unfortunately I still couldn't get it to work. Given what I have here, could anybody give me a push in the right direction to get live filtering working?

Any help would be greatly appreciated.

Just out of curiosity, is this really a difficult task? If so, it seems that the people who designed ICollectionViewLiveShaping did a pretty poor job of it.

Update: It appears that the only way to add a property to ICollectionViewLiveShaping's LiveFilteringProperties collection is via a string. Given that limitation, is it even possible to filter by properties in another class (Workers' EmployerID in this case)?

Can anybody who has any experience with ICollectionViewLiveShaping tell me if what I'm trying to do in this situation is even a viable option? I honestly don't know if it is or not due to the utter lack of documentation and examples available. Even if it's not feasible it would at least be good to know if I'm wasting my time or not.

回答1:

All you need to do is add a property in LiveFilteringProperties for which you want the filter to call on property change and set IsLiveFiltering to true for your collection to enable live filtering.

Make sure PropertyChanged event gets raised whenever EmployerID property changes i.e. your Worker class should implement INotifyPropertyChangedEvent.

This will work then -

public ICollectionViewLiveShaping WorkersEmployed { get; set; }

ICollectionView workersCV = new CollectionViewSource
                         { Source = GameContainer.Game.Workers }.View;

ApplyFilter(workersCV);

WorkersEmployed = workersCV as ICollectionViewLiveShaping;
if (WorkersEmployed.CanChangeLiveFiltering)
{
    WorkersEmployed.LiveFilteringProperties.Add("EmployerID");
    WorkersEmployed.IsLiveFiltering = true;
}


回答2:

I experimented with this and it looks like it is not designed for what you (and me) want: Automatic filtering when you change filtering conditions. It filters automatically when some properties of DataGrid's item source changes, but not when filter conditions change - you must call ICollectionViewSource.Refresh manually.