自动刷新的ICollectionView过滤器 [英] Automatically refresh ICollectionView Filter

查看:312
本文介绍了自动刷新的ICollectionView过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有什么办法,而不必调用刷新()时,自动更新一个的ICollectionView 过滤器?有关更改已经完成

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

我有以下内容:

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

[通知]在这个属性属性只农具 INotifyPropertyChanged的,但它似乎并没有做在这种情况下任何东西。

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;
    };

在XAML:

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



更新:它看起来像使用的ICollectionView 是要对我来说是必要的,所以我想讨论这个主题。我加入悬赏这个问题,它的收件人将是任何人谁可以提供关于如何实现'放手'的ICollectionView 那并不是一些见解吨需要手动刷新。在这一点上,我愿意接受任何的想法。

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.

推荐答案

据我所知有中没有内置支持的ICollectionView 刷新在底层源收集的任何属性更改集合。

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

不过,你也可以继承的ListCollectionView 给它自己的实现,以刷新更改的任何属性集合。样本 -

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);

这可以在您的项目,而不必担心刷新每的PropertyChanged 。 MyCollectionView 将这样做自动为您服务。

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

如果您正在使用的 .Net4.5 您可以用 ICollectionViewLiveShaping 描述实施的这里

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

我已张贴有关问题的实现部分在这里 - 的实施ICollectionViewLiveShaping

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;
}

这篇关于自动刷新的ICollectionView过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆