WPF绑定过滤的ObservableCollection的ICollectionView到组合框 [英] WPF Binding filtered ObservableCollection ICollectionView to Combobox

查看:1231
本文介绍了WPF绑定过滤的ObservableCollection的ICollectionView到组合框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要过滤一个ObservableCollection根据类型(Type AddPoint)的一个子集,并希望它排序,没有重复上升。我的基类是模型库,W /子类AddPoint,时间,重复,等...的ObservableCollection MotionSequenceCollection会以任意顺序填写W /这些类型有的会重复。



我试过几个不同的时间,他们在物业的ICollectionView从我'拉'如下图所示:的收集的绑定子集。



观察集合

 私人的ObservableCollection<模型库> _motionSequenceCollection = 
新的ObservableCollection<&模型库GT;();

公众的ObservableCollection<&模型库GT; MotionSequenceCollection
{
得到
{
返回_motionSequenceCollection;
}


{
如果(_motionSequenceCollection ==值)
{
的回报;
}

VAR属性oldValue = _motionSequenceCollection;
_motionSequenceCollection =价值;

//更新绑定,没有广播
RaisePropertyChanged();
}
}

公众的ICollectionView位置
{
得到
{
变种位置= CollectionViewSource.GetDefaultView(_motionSequenceCollection);

//不起作用。问题:的GetType()创建的System.Type()和AddPoint,这不工作的类型。需要打石膏,或什么?
//在http://stackoverflow.com/questions/9621393/bind-subset-of-collection中发现的问题是,有一个错误:
//不能申请运营商'=='来类型'的System.Type'和'MotionSeq.Model.AddPoint',
的操作数//候选人是:
//布尔==(System.Reflection.MemberInfo,System.Reflection.memberInfo)(以一流的MemberInfo)
//布尔==(System.Type的,System.Type的)(类型)
//location.Filter = p => (对作为模型库).GetType()== AddPoint;

//不起作用。问题:影响主要收集,不会让补时类型。
//location.Filter = 01 => (O1是AddPoint);

//不起作用。问题:排序好的,但也排序MotionSequenceCollection!什么瓦特/那!?
//location.SortDescriptions.Add(new SortDescription(AddPointClassName,ListSortDirection.Ascending));

//不起作用。问题:MotionSequenceCollection不更新。
//location.Filter = P => (P为模型库)== AddPoint;

//不起作用。问题:源未初始化,从计算器〔实施例和不知道如何在第一时间到达那里(?)。
//source.Filter = P => (P为模型库).GetType()==AddPoint
//返回源;

返回位置;
}
}


解决方案

所有藏品都有一个默认的CollectionView。 WPF总是绑定到一个视图,而不是一个集合。如果你直接绑定到一个集合,实际上WPF绑定到该集合的默认视图。此默认视图是由所有绑定到集合,这将导致所有直接绑定到集合共享排序,过滤,分组,和一个默认视图当前项目的特点共享。




尝试创建的 CollectionViewSource 并设置其过滤逻辑是这样的:

  / /它创建为静态资源和绑定你的ItemsControl它
< CollectionViewSource X:键=CSV来源={StaticResource的MotionSequenceCollection}
滤波器=CollectionViewSource_Filter>
< CollectionViewSource.GroupDescriptions>
< PropertyGroupDescription属性名=YYY/>
< /CollectionViewSource.GroupDescriptions>
< CollectionViewSource.SortDescriptions>
< SCM:SortDescription属性名=YYY方向=升序/>
< /CollectionViewSource.SortDescriptions>
< / CollectionViewSource>

私人无效CollectionViewSource_Filter(对象发件人,FilterEventArgs E)
{
变种T = e.Item作为模型库;
如果(T!= NULL)

{
//使用过滤逻辑在这里

}
}


I want to filter an ObservableCollection to a subset based on type (type AddPoint) and want it ordered ascending with no duplicates. My base class is ModelBase, w/ sub-classes AddPoint, Time, Repeat, etc... The ObservableCollection MotionSequenceCollection will be filled w/ those types in any order and some will be duplicates.

I've tried several different times and shown them below in the ICollectionView Property that I 'pulled' from: Bind subset of collection.

OBSERVABLE COLLECTION

private ObservableCollection<ModelBase> _motionSequenceCollection = 
        new ObservableCollection<ModelBase>();

    public ObservableCollection<ModelBase> MotionSequenceCollection
    {
        get
        {
            return _motionSequenceCollection;
        }

        set
        {
            if (_motionSequenceCollection == value)
            {
                return;
            }

            var oldValue = _motionSequenceCollection;
            _motionSequenceCollection = value;

            // Update bindings, no broadcast
            RaisePropertyChanged();
        }
    }

    public ICollectionView Location
    {
        get
        {
             var location = CollectionViewSource.GetDefaultView(_motionSequenceCollection);

            //DOES NOT WORK.  PROBLEM: GetType() creates type of system.type() and AddPoint, which don't work.  Need a cast, or something??  
            // found at http://stackoverflow.com/questions/9621393/bind-subset-of-collection  The problem is that there is an error:
            //    Cannot apply operator '==' to operands of type 'System.Type' and 'MotionSeq.Model.AddPoint',
            //    candidates are:
            //          bool ==(System.Reflection.MemberInfo, System.Reflection.memberInfo) (in class MemberInfo)
            //          bool ==(System.type, System.Type) (in class Type)
            //location.Filter = p => (p as ModelBase).GetType() == AddPoint;

            //DOES NOT WORK.  PROBLEM: Affects the main collection and won't let TIME type added.
            //location.Filter = o1 => (o1 is AddPoint);

            //DOES NOT WORK.  PROBLEM: Sorts fine, but also sorts MotionSequenceCollection!!  What up w/ that!?  
            //location.SortDescriptions.Add(new SortDescription("AddPointClassName", ListSortDirection.Ascending));

            //DOES NOT WORK.  PROBLEM: MotionSequenceCollection does not update.
            //location.Filter = p => (p as ModelBase) == AddPoint;

            //DOES NOT WORK.  PROBLEM: Source is not instantiated(?) and exmaple from stackoverflow and not sure how that got there in the first place.
            //source.Filter = p => (p as ModelBase).GetType() == "AddPoint";
            //return source;

            return location;
        }
    }

解决方案

All collections have a default CollectionView. WPF always binds to a view rather than a collection. If you bind directly to a collection, WPF actually binds to the default view for that collection. This default view is shared by all bindings to the collection, which causes all direct bindings to the collection to share the sort, filter, group, and current item characteristics of the one default view.

try creating CollectionViewSource and setting its filtering logic like this:

//create it as static resource and bind your ItemsControl to it
<CollectionViewSource x:Key="csv" Source="{StaticResource MotionSequenceCollection}" 
                  Filter="CollectionViewSource_Filter">
    <CollectionViewSource.GroupDescriptions>
       <PropertyGroupDescription PropertyName="YYY"/>
    </CollectionViewSource.GroupDescriptions>
    <CollectionViewSource.SortDescriptions>
         <scm:SortDescription PropertyName="YYY" Direction="Ascending"/>
    </CollectionViewSource.SortDescriptions>
</CollectionViewSource> 

private void CollectionViewSource_Filter(object sender, FilterEventArgs e)
{
    var t = e.Item as ModelBase;
    if (t != null)

    {
        //use your filtering logic here

    }
}

这篇关于WPF绑定过滤的ObservableCollection的ICollectionView到组合框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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