Automatically refresh ICollectionView Filter

孤者浪人 提交于 2019-11-28 20:34:19

问题


Is there any way to automatically update a filter on an ICollectionView without having to call Refresh() when a relevant change has been made?

I have the following:

[Notify]
public ICollectionView Workers { get; set; }

The [Notify] attribute in this property just implements INotifyPropertyChanged but it doesn't seem to be doing anything in this situation.

Workers = new CollectionViewSource { Source = DataManager.Data.Workers }.View;

Workers.Filter = w =>
    {
        Worker worker = w as Worker;
        if (w == null)
            return false;
        return worker.Employer == this;
    };

In XAML:

<TextBlock x:Name="WorkersTextBlock"
           DataContext="{Binding PlayerGuild}"
           FontFamily="Pericles"
           Text="{Binding Workers.Count,
                          StringFormat=Workers : {0},
                          FallbackValue=Workers : 99}" />

Update: It looks like using ICollectionView is going to be necessary for me, so I'd like to revisit this topic. I'm adding a bounty to this question, the recipient of which will be any person who can provide some insight on how to implement a 'hands-off' ICollectionView that doesn't need to be manually refreshed. At this point I'm open to any ideas.


回答1:


AFAIK there is no inbuilt support in ICollectionView to refresh collection on any property change in underlying source collection.

But you can subclass ListCollectionView to give it your own implementation to refresh collection on any property changed. Sample -

public class MyCollectionView : ListCollectionView
{
    public MyCollectionView(IList sourceCollection) : base(sourceCollection)
    {
        foreach (var item in sourceCollection)
        {
            if (item is INotifyPropertyChanged)
            {
                ((INotifyPropertyChanged)item).PropertyChanged +=
                                                  (s, e) => Refresh();
            }
        }
    }
}

You can use this in your project like this -

Workers = new MyCollectionView(DataManager.Data.Workers);

This can be reused across your project without having to worry to refresh collection on every PropertyChanged. MyCollectionView will do that automatically for you.

OR

If you are using .Net4.5 you can go with ICollectionViewLiveShaping implementation as described here.

I have posted the implementation part for your problem here - Implementing ICollectionViewLiveShaping.

Working code from that post -

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:


For .Net 4.5: There is a new interface which can help to achieve this feature, called : ICollectionViewLiveShaping.

From MSDN link:

When live sorting, grouping, or filtering is enabled, a CollectionView will rearrange the position of data in the CollectionView when the data is modified. For example, suppose that an application uses a DataGrid to list stocks in a stock market and the stocks are sorted by stock value. If live sorting is enabled on the stocks' CollectionView, a stock's position in the DataGrid moves when the value of the stock becomes greater or less than another stock's value.

More Info on above interface: http://www.jonathanantoine.com/2011/10/05/wpf-4-5-%E2%80%93-part-10-live-shaping/


For .Net 4 and lower: There is also another post on SO QA which might help you: CollectionViewSource Filter not refreshed when Source is changed



来源:https://stackoverflow.com/questions/17735994/automatically-refresh-icollectionview-filter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!