ItemsControl和CollectionViewSource [英] ItemsControl and CollectionViewSource

查看:60
本文介绍了ItemsControl和CollectionViewSource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!

我想过滤ObservableCollection<>在将其绑定到ItemsControl.ItemsSource之前.

这是我使用的代码:



Hello!

I want to filter ObservableCollection<> before binding it to ItemsControl.ItemsSource.

Here the code I use:



private ObservableCollection<IControlableModel> _controls = null;
public ObservableCollection<IControlableModel> Controls
{
    get
    {
        if (_controls == null)
        {
            _controls = new ObservableCollection<IControlableModel>();
        }
        return _controls;
    }
    set
    {
        _controls = value;
        OnPropertyChanged("Controls");

    }
}

CollectionViewSource _notMaskedControls = null;
public CollectionViewSource NotMaskedControls
{
    get
    {
        if (_notMaskedControls == null)
        {
            _notMaskedControls = new CollectionViewSource() { Source = Controls };
        }
        return _notMaskedControls;
    }
}


如果将ItemsControl.ItemsSource绑定到控件,则可以看到所有项目,但是如果将相同的ItemsSource绑定到NotMaskedControls,则不会出现任何项目.

请帮助


If I bind ItemsControl.ItemsSource to Controls I see all the Items, but if I bind the same ItemsSource to NotMaskedControls no items appears.

Please help

推荐答案

移动注释以回答.

对此我不确定,但您是否不想绑定到CollectionViewSource视图或Source属性"

解决方法是绑定到View属性.
Moving comment to answer.

"I am not sure about this but would you not want to bind to the CollectionViewSource View or Source property"

The resolution was to bind to the View property.


您必须完全不同.
您确实将observablecollection绑定到collectionviewsource,但是您对其应用了过滤器. Collectionviewsource具有可分配的过滤器属性,因此,只会显示与过滤器匹配的一些元素.
You have to do this entirely different.
You do bind the observablecollection to the collectionviewsource, but you apply a filter on it. Collectionviewsource has a filter property that you can assign and as such only a few elements that match the filter will be shown.


这篇关于ItemsControl和CollectionViewSource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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