c# – WPF将已过滤的ObservableCollection ICollectionView绑定到Combobox

c# – WPF将已过滤的ObservableCollection ICollectionView绑定到Combobox,第1张

概述我想根据类型(类型AddPoint)过滤ObservableCollection到一个子集,并希望它按升序排序,没有重复.我的基类是ModelBase,w /子类AddPoint,Time,Repeat等…… ObservableCollection MotionSequenceCollection将以任何顺序填充这些类型,有些将是重复的. 我已经尝试了几次不同的时间,并在ICollectionV 我想根据类型(类型AddPoint)过滤ObservableCollection到一个子集,并希望它按升序排序,没有重复.我的基类是ModelBase,w /子类AddPoint,Time,Repeat等…… ObservableCollection MotionSequenceCollection将以任何顺序填充这些类型,有些将是重复的.

我已经尝试了几次不同的时间,并在ICollectionVIEw属性中显示它,我从’Bind subset of 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 https://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.

尝试创建CollectionViewSource并设置其过滤逻辑,如下所示:

//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    }}
总结

以上是内存溢出为你收集整理的c# – WPF将已过滤的ObservableCollection ICollectionView绑定到Combobox全部内容,希望文章能够帮你解决c# – WPF将已过滤的ObservableCollection ICollectionView绑定到Combobox所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1252815.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存