/// <summary> /// Construct a sorter. /// </summary> /// <param name="keyGetter">The function that extracts a key from items in the list.</param> /// <param name="collection">The collection to be sorted.</param> /// <param name="dispatcher">The dispatcher on which move operations can be performed.</param> /// <param name="orderCheckTriggerProperties">The names of the properties of items that cause ordering to be checked when they change.</param> /// <param name="collectionAccessMediator">An object capable of preventing concurrent access to the target collection while the sorter performs the initial sort.</param> public CollectionAutoSorter(Func <TItem, TKey> keyGetter, ObservableCollection <TItem> collection, Dispatcher dispatcher, IEnumerable <string> orderCheckTriggerProperties, IObservableCollectionAccessMediator collectionAccessMediator, bool orderDescending) { _keyGetter = keyGetter; _collection = collection; _dispatcher = dispatcher; _collection.CollectionChanged += HandleCollectionChanged; _order = new CollectionOrder <TItem, TKey>(keyGetter, collection, dispatcher, orderCheckTriggerProperties, orderDescending); _order.LoadKeysAndTriggerArrange(collection, collectionAccessMediator); }
/// <summary> /// Specify a function that extracts a sortable key from object instances, that can be used to order items in the view. /// /// The ordering of the view will be checked whenever one of the items raises a property changed event /// (see <see cref="INotifyPropertyChanged"/>) for one of a set of specified properties. Only the position of the element /// raising the event will be considered. /// /// When new items are added to the collection, the function will be used to select an appropriate position /// in the view. /// </summary> /// <param name="function">The function that gets the sort key from an instance of <see cref="T"/>.</param> /// <param name="orderCheckPropertyNames">The names of the properties that should trigger a check.</param> public void OrderBy <TKey>(Func <T, TKey> function, IEnumerable <string> orderCheckPropertyNames, bool orderDescending) where TKey : IComparable { if (_mediator == null) { throw new MediatorNotSetException(); } if (_collectionOrder != null) { _collectionOrder.Detach(); } var order = new CollectionOrder <T, TKey>(function, View, _dispatcher, orderCheckPropertyNames, orderDescending); order.LoadKeysAndTriggerArrange(_target, _mediator); _collectionOrder = order; }
/// <summary> /// Specify a function that extracts a sortable key from object instances, that can be used to order items in the view. /// /// The ordering of the view will be checked whenever one of the items raises a property changed event /// (see <see cref="INotifyPropertyChanged"/>) for one of a set of specified properties. Only the position of the element /// raising the event will be considered. /// /// When new items are added to the collection, the function will be used to select an appropriate position /// in the view. /// </summary> /// <param name="function">The function that gets the sort key from an instance of <see cref="T"/>.</param> /// <param name="orderCheckPropertyNames">The names of the properties that should trigger a check.</param> /// <param name="orderDescending">True if the sort order is to be descending instead of ascending</param> public void OrderBy <TKey>(Func <TItem, TKey> function, IEnumerable <string> orderCheckPropertyNames, bool orderDescending) where TKey : IComparable { _log.LazyAdd("Set Order {0}", new Lazy <string>(function.ToString), new Lazy <string>(() => orderCheckPropertyNames.Aggregate((t, i) => t + "," + i))); if (_mediator == null) { throw new MediatorNotSetException(); } if (_collectionOrder != null) { _collectionOrder.Detach(); } var order = new CollectionOrder <TItem, TKey>(function, View, _dispatcher, orderCheckPropertyNames, orderDescending); order.LoadKeysAndTriggerArrange(View, _mediator); _collectionOrder = order; }