Beispiel #1
0
        /// <summary>
        /// Moves the given items to the front of the list.
        /// </summary>
        /// <typeparam name="TItem">The type of the items.</typeparam>
        /// <param name="this">This BindingList.</param>
        /// <param name="items">The items.</param>
        public static void MoveToFront <TItem>(this BindingList <TItem> @this, IEnumerable <TItem> items)
        {
#if NET40
            Contract.Requires(@this != null);
            Contract.Requires(items != null);
#endif
            var raiseEvents = @this.RaiseListChangedEvents;
            try {
                @this.RaiseListChangedEvents = false;
                foreach (var item in items.Reverse())
                {
                    @this.Remove(item);
                    @this.Insert(0, item);
                }
            } finally {
                @this.RaiseListChangedEvents = raiseEvents;
                @this.ResetBindings();
            }
        }
Beispiel #2
0
        /// <summary>
        /// Refreshes all items int list.
        /// </summary>
        /// <typeparam name="T">The type of the items</typeparam>
        /// <param name="this">This BindingList.</param>
        /// <param name="items">The updated item list.</param>
        /// <param name="keyGetter">The key getter to compare what items are added/removed/updated.</param>
        /// <param name="itemUpdateMethod">The item update method; takes the old and new item reference and returns the updated item.</param>
        public static void RefreshAll <T>(this BindingList <T> @this, IEnumerable <T> items, Func <T, string> keyGetter, Func <T, T, T> itemUpdateMethod)
        {
            var oldState = @this.RaiseListChangedEvents;

            try {
                @this.RaiseListChangedEvents = false;
                var oldKeys = @this.ToDictionary(keyGetter);
                var newKeys = items.ToDictionary(keyGetter);

                // remove not longer needed items
                foreach (var key in oldKeys.Keys.Where(k => !newKeys.ContainsKey(k)))
                {
                    @this.Remove(oldKeys[key]);
                }

                foreach (var key in newKeys.Keys)
                {
                    // add new items
                    if (!oldKeys.ContainsKey(key))
                    {
                        @this.Add(newKeys[key]);
                        continue;
                    }

                    // update items
                    var oldItem = oldKeys[key];
                    var newItem = itemUpdateMethod(oldItem, newKeys[key]);
                    if (!ReferenceEquals(oldItem, newItem))
                    {
                        @this[@this.IndexOf(oldItem)] = newItem;
                    }
                }
            } finally {
                @this.RaiseListChangedEvents = oldState;
                if (oldState)
                {
                    @this.ResetBindings();
                }
            }
        }
    /// <summary>
    /// Adds the elements of the specified collection to the end of the <see cref="System.ComponentModel.BindingList{T}"/>,
    /// while only firing the <see cref="System.ComponentModel.BindingList{T}.ListChanged"/>-event once.
    /// </summary>
    /// <typeparam name="T">
    /// The type T of the values of the <see cref="System.ComponentModel.BindingList{T}"/>.
    /// </typeparam>
    /// <param name="bindingList">
    /// The <see cref="System.ComponentModel.BindingList{T}"/> to which the values shall be added.
    /// </param>
    /// <param name="collection">
    /// The collection whose elements should be added to the end of the <see cref="System.ComponentModel.BindingList{T}"/>.
    /// The collection itself cannot be null, but it can contain elements that are null,
    /// if type T is a reference type.
    /// </param>
    /// <exception cref="ArgumentNullException">values is null.</exception>
    public static void AddRange <T>(this System.ComponentModel.BindingList <T> bindingList, IEnumerable <T> collection)
    {
        // The given collection may not be null.
        if (collection == null)
        {
            throw new ArgumentNullException(nameof(collection));
        }

        if (!collection.Any())
        {
            return;
        }

        // Remember the current setting for RaiseListChangedEvents
        // (if it was already deactivated, we shouldn't activate it after adding!).
        var oldRaiseEventsValue = bindingList.RaiseListChangedEvents;

        // Try adding all of the elements to the binding list.
        try
        {
            bindingList.RaiseListChangedEvents = false;

            foreach (var value in collection)
            {
                bindingList.Add(value);
            }
        }

        // Restore the old setting for RaiseListChangedEvents (even if there was an exception),
        // and fire the ListChanged-event once (if RaiseListChangedEvents is activated).
        finally
        {
            bindingList.RaiseListChangedEvents = oldRaiseEventsValue;

            if (bindingList.RaiseListChangedEvents)
            {
                bindingList.ResetBindings();
            }
        }
    }
Beispiel #4
0
        /// <summary>
        /// Replaces all elements.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="this">This BindingList.</param>
        /// <param name="items">The items.</param>
        public static void ReplaceAll <T>(this BindingList <T> @this, IEnumerable <T> items)
        {
#if NET40
            Contract.Requires(@this != null);
            Contract.Requires(items != null);
#endif
            var oldState = @this.RaiseListChangedEvents;
            try {
                @this.RaiseListChangedEvents = false;
                @this.Clear();
                foreach (var item in items)
                {
                    @this.Add(item);
                }
            } finally {
                @this.RaiseListChangedEvents = oldState;
                if (oldState)
                {
                    @this.ResetBindings();
                }
            }
        }
Beispiel #5
0
        /// <summary>
        /// Moves the given items relative.
        /// </summary>
        /// <typeparam name="TItem">The type of the items</typeparam>
        /// <param name="this">This BindingList.</param>
        /// <param name="items">The items.</param>
        /// <param name="delta">The delta.</param>
        public static void MoveRelative <TItem>(this BindingList <TItem> @this, IEnumerable <TItem> items, int delta)
        {
#if NET40
            Contract.Requires(@this != null);
            Contract.Requires(items != null);
#endif
            if (delta == 0)
            {
                return;
            }

            var raiseEvents = @this.RaiseListChangedEvents;
            try {
                @this.RaiseListChangedEvents = false;

                var count = @this.Count - 1;

                if (delta < 0)
                {
                    var start = 0;
                    foreach (var item in items)
                    {
                        var index = @this.IndexOf(item);
                        if (index < 0)
                        {
                            continue;
                        }

                        @this.RemoveAt(index);
                        var newIndex = index + delta;
                        @this.Insert(newIndex < 0 ? start++ : newIndex, item);
                    }
                }
                else
                {
                    var end = count;
                    foreach (var item in items.Reverse())
                    {
                        var index = @this.IndexOf(item);
                        if (index < 0)
                        {
                            continue;
                        }

                        @this.RemoveAt(index);
                        var newIndex = index + delta;
                        if (newIndex > end)
                        {
#if NET40
                            Contract.Assume(end >= 0);
#endif
                            @this.Insert(end--, item);
                        }
                        else
                        {
                            @this.Insert(newIndex, item);
                        }
                    }
                }
            } finally {
                @this.RaiseListChangedEvents = raiseEvents;
                @this.ResetBindings();
            }
        }
 private void propertyGrid_PropertyValueChanged(object s, PropertyValueChangedEventArgs e)
 {
     //Обновляем информацию в ListBox при изменениях в PropertyGrid
     _list.ResetBindings();
 }