我想根据类型(类型 AddPoint)将 ObservableCollection 过滤到一个子集,并希望它按升序排列,没有重复。我的基类是 ModelBase,带有子类 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;
}
}