Tôi muốn lọc một ObservableCollection thành một tập con dựa trên loại (loại AddPoint) và muốn nó được sắp xếp tăng dần không có trùng lặp. Lớp cơ sở của tôi là ModelBase, w/sub-classes AddPoint, Time, Repeat, vv ... ObservableCollection MotionSequenceCollection sẽ được lấp đầy với các loại đó theo thứ tự bất kỳ và một số sẽ là bản sao.WPF Binding lọc ObservableCollection ICollectionView đến Combobox
Tôi đã thử nhiều lần khác nhau và hiển thị chúng bên dưới trong Thuộc tính ICollectionView mà tôi đã kéo 'từ: Bind subset of collection.
quan sát THU
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;
}
}
Thành thật mà nói, tôi đang đấu tranh ồ ạt với điều này. Ban đầu tôi đã đi @ makc cách w/CollectionViewSource_FIlter và tìm thấy một vài ví dụ để giúp đỡ, nhưng vấn đề là làm cho danh sách riêng biệt() - nó đã được 2 ngày trước và đầu của tôi là sương mù. Bộ lọc giống như sau: – FloppyDisk
Tôi cần sửa đổi nhận xét của mình. Tôi đã nhận được giải pháp của bạn làm việc và học hỏi rất nhiều về collectionviewsource .. Vấn đề là nếu có bản sao và chỉ có một chương trình trong combobox (xem lọc), khi tôi chỉnh sửa một trong bộ sưu tập, nó không cập nhật bằng cách thêm mới đã chỉnh sửa bản ghi. Nếu chỉ có một, thì cập nhật đó như mong đợi sau khi đã thay đổi. Đó là lý do tại sao tôi đã đi xuống một con đường linq sau, nhưng điều đó đã được chứng minh là thảm họa. – FloppyDisk
@FloppyDisk Tôi rất vui vì bạn đã không sao chép quá khứ nhưng đã đọc và đã học :), tôi không chắc chắn tôi hiểu được vấn đề hoàn toàn cố gắng tạo ra một ví dụ nhỏ đứng một mình và tôi nghĩ rằng nó có giá trị một câu hỏi mới ... – makc