/// <summary>
        /// Sorts the collection based on a specified property name.
        /// </summary>
        /// <param name="propertyName">Name of the property.</param>
        /// <param name="descending">if set to <c>true</c>, then sorts the collection in descending order.</param>
        public void Sort(string propertyName, bool descending)
        {
            // Get list to sort
            var items = Items as List <T>;

            // Apply and set the sort, if items to sort
            if (items != null)
            {
                var pc = new PropertyComparer <T>(propertyName, descending);
                items.Sort(pc);
            }
        }
示例#2
0
        /// <summary>
        /// Sorts the items if overridden in a derived class; otherwise, throws a <see cref="T:System.NotSupportedException"/>.
        /// </summary>
        /// <param name="prop">A <see cref="T:System.ComponentModel.PropertyDescriptor"/> that specifies the property to sort on.</param>
        /// <param name="direction">One of the <see cref="T:System.ComponentModel.ListSortDirection"/>  values.</param>
        /// <exception cref="T:System.NotSupportedException">Method is not overridden in a derived class. </exception>
        protected override void ApplySortCore(PropertyDescriptor prop, ListSortDirection direction)
        {
            _sortProperty  = prop;
            _sortDirection = direction;

            // Get list to sort
            List <T> items = Items as List <T>;

            // Apply and set the sort, if items to sort
            if (items != null)
            {
                PropertyComparer <T> pc = new PropertyComparer <T>(prop, direction);
                items.Sort(pc);
                _isSorted = true;
            }
            else
            {
                _isSorted = false;
            }

            // Let bound controls know they should refresh their views
            OnListChanged(new ListChangedEventArgs(ListChangedType.Reset, -1));
        }