简体   繁体   English

自动刷新ICollectionView过滤器

[英]Automatically refresh ICollectionView Filter

Is there any way to automatically update a filter on an ICollectionView without having to call Refresh() when a relevant change has been made? 是否有任何方法可以自动更新ICollectionView上的过滤器,而无需在进行相关更改时调用Refresh()

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. 此属性中的[Notify]属性只实现了INotifyPropertyChanged但在这种情况下它似乎没有做任何事情。

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: 在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. 更新:看起来使用ICollectionView对我来说是必要的,所以我想重新审视这个主题。 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. 我正在为这个问题添加一个赏金,其中的收件人将是任何能够提供有关如何实现不需要手动刷新的“放手” ICollectionView洞察力的人。 At this point I'm open to any ideas. 在这一点上,我对任何想法持开放态度。

AFAIK there is no inbuilt support in ICollectionView to refresh collection on any property change in underlying source collection. AFAIK在ICollectionView没有内置支持来刷新基础源集合中任何属性更改的集合。

But you can subclass ListCollectionView to give it your own implementation to refresh collection on any property changed . 但是你可以ListCollectionView给它自己的实现来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 . 这可以在整个项目中重复使用,而不必担心每个PropertyChanged上的刷新集合。 MyCollectionView will do that automatically for you. MyCollectionViewautomatically为您完成。

OR 要么

If you are using .Net4.5 you can go with ICollectionViewLiveShaping implementation as described here . 如果您使用的是.Net4.5 ,则可以使用此处所述的ICollectionViewLiveShaping实现。

I have posted the implementation part for your problem here - Implementing ICollectionViewLiveShaping . 我已经在这里发布了您的问题的实现部分 - 实现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;
}

For .Net 4.5: There is a new interface which can help to achieve this feature, called : ICollectionViewLiveShaping . 对于.Net 4.5:有一个新的界面可以帮助实现这个功能,称为: ICollectionViewLiveShaping

From MSDN link : 来自MSDN链接

When live sorting, grouping, or filtering is enabled, a CollectionView will rearrange the position of data in the CollectionView when the data is modified. 启用实时排序,分组或过滤后,CollectionView将在修改数据时重新排列CollectionView中数据的位置。 For example, suppose that an application uses a DataGrid to list stocks in a stock market and the stocks are sorted by stock value. 例如,假设应用程序使用DataGrid列出股票市场中的股票,并且股票按股票价值排序。 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. 如果在股票的CollectionView上启用了实时排序,当股票的价值变得大于或小于另一个股票的价值时,股票在DataGrid中的位置就会移动。

More Info on above interface: http://www.jonathanantoine.com/2011/10/05/wpf-4-5-%E2%80%93-part-10-live-shaping/ 有关上述界面的更多信息: 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 对于.Net 4及更低版本 :SO QA上还有另一篇文章可能会对您有所帮助: 当Source被更改时,CollectionViewSource Filter不会刷新

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM