How to handle CompositeCollection with CollectionView functions?

Is there a way to get notified when the current location of the CompositeCollection changes?

I need CompositeCollection to be controlled by CollectionView, any ideas are welcome.

+6
wpf observablecollection collectionviewsource
source share
3 answers

You can detect when the current item has changed by tracking the ICollectionView.CurrentChanged event of your CollectionView. The following code works for me:

 CompositeCollection cc = new CompositeCollection(); cc.Add(new CollectionContainer { Collection = new string[] { "Oh No!", "Fie" } }); cc.Add(new CollectionContainer { Collection = new string[] { "Zounds", "Ods Bodikins" } }); CollectionViewSource cvs = new CollectionViewSource { Source = cc }; // Subscribing to CurrentChanged on the ICollectionView cvs.View.CurrentChanged += (o, e) => MessageBox.Show("current changed"); lb.ItemsSource = cvs.View; // lb is a ListBox with IsSynchronizedWithCurrentItem="True" 

When I change the selection in the ListBox, a message box appears.

As for filtering, sorting and grouping, according to Aron's answer, they are not available when viewing over CompositeCollection. But for the record here, you may find changes to views that support these functions:

  • It looks like you will get the CollectionChanged event when the filter changes, although I cannot find this document.
  • SortDescriptions is a SortDescriptionCollection, which is an INotifyCollectionChanged, so hook up the CollectionChanged event handler in the SortDescriptions property.
  • GroupDescriptions ObservableCollection<GroupDescription> , so enable the CollectionChanged event handler in the GroupDescriptions property.
+6
source share

You cannot run CollectionView in copmposite collection, see here

0
source share

I ran into the same problem: I need a CompositeCollection collation. I wrote the following class that solves the problem, at least for ObservableCollections of the same type.

The idea is to maintain the composite collection as a regular observable collection and update it as the base collections change. Then the resulting set (AllNodes) can be used in the user interface, and it perfectly supports CollectionView.

 using System.Collections.ObjectModel; using System.Collections.Specialized; using System.Linq; namespace Util { public class ObservableCollectionCollector<T> { private class ReplacableObservableCollection : ObservableCollection<T> { public void Replace(int idx, T v) { SetItem(idx, v); } } private readonly ReplacableObservableCollection allNodes; private readonly ObservableCollection<T>[] colls; private readonly int[] lens; public ObservableCollectionCollector(params ObservableCollection<T>[] colls) { this.colls = colls; allNodes = new ReplacableObservableCollection(); foreach (var l in colls) { foreach (var e in l) allNodes.Add(e); l.CollectionChanged += HandleCollectionChanged; } lens = colls.Select(c => c.Count).ToArray(); } public ReadOnlyObservableCollection<T> AllNodes { get { return new ReadOnlyObservableCollection<T>(allNodes); } } private void HandleCollectionChanged(object sender, NotifyCollectionChangedEventArgs e) { int i0 = 0; int ci = 0; foreach (var l in colls) { if (l == sender) break; i0 += l.Count; ++ci; } switch (e.Action) { case NotifyCollectionChangedAction.Add: for (int i = 0; i < e.NewItems.Count; ++i) allNodes.Insert(i0 + e.NewStartingIndex + i, (T)e.NewItems[i]); break; case NotifyCollectionChangedAction.Move: for (int i = 0; i < e.OldItems.Count; ++i) allNodes.Move(i0 + e.OldStartingIndex + i, i0 + e.NewStartingIndex + i); break; case NotifyCollectionChangedAction.Remove: for (int i = 0; i < e.OldItems.Count; ++i) allNodes.RemoveAt(i0 + e.OldStartingIndex); break; case NotifyCollectionChangedAction.Replace: for (int i = 0; i < e.NewItems.Count; ++i) allNodes.Replace(i0 + e.OldStartingIndex + i, (T)e.NewItems[i]); break; case NotifyCollectionChangedAction.Reset: for (int i = 0; i < lens[ci]; ++i) allNodes.RemoveAt(i0); break; } lens[ci] = ((ObservableCollection<T>)sender).Count; } } } 
0
source share

All Articles