Collection Editor

Mar 14, 2012 at 12:39 AM

Great work on this control, I found a use while creating a custom properties editor (long story why i didnt use the built in version).

Now one thing I did end up doing was creating three events for the create, deleting and deleted. I am not sure if you will implement these events but makes a simple method for manipulating the underlying items.

Here are the events. (added to collectioneditor.cs)

        #region Events
        /// <summary>
        /// The Item Deleting Event Handler. This handler exposes the new item being created with the ability to modify \ cancel the delete request
        /// </summary>
        /// <param name="sender">the collection editor</param>
        /// <param name="e">the item deleting event args</param>
        /// <remarks>
        ///     Created by NVH in version 1.6.1
        /// </remarks>
        public delegate void ItemDeletingEventHandler(object sender, ItemDeletingEventArgs e);
        
        /// <summary>
        /// The Item Deleted Event Handler. A simple event handler once an item is removed from the collection
        /// </summary>
        /// <param name="sender">The collection editor</param>
        /// <param name="e">generic event args</param>
        /// <remarks>
        ///     Created by NVH in version 1.6.1
        /// </remarks>
        public delegate void ItemDeletedEventHandler(object sender, EventArgs e);

        /// <summary>
        /// The Item Created Event handler. This handler exposes the new item being created with the ability to modify \ cancel the create request
        /// </summary>
        /// <param name="sender">the collection editor</param>
        /// <param name="e">the new item create event args</param>
        /// <remarks>
        ///     Created by NVH in version 1.6.1
        /// </remarks>
        public delegate void NewItemCreatedEventHandler(object sender, NewItemCreatedEventArgs e);

        /// <summary>
        /// The Item Deleting Event. This event exposes the new item being created with the ability to modify \ cancel the delete request
        /// </summary>
        /// <remarks>
        ///     Created by NVH in version 1.6.1
        /// </remarks>
        public event ItemDeletingEventHandler OnItemDeleting;

        /// <summary>
        /// The Item Deleted Event. A simple event once an item is removed from the collection
        /// </summary>
        /// <remarks>
        ///     Created by NVH in version 1.6.1
        /// </remarks>
        public event ItemDeletedEventHandler OnItemDeleted;

        /// <summary>
        /// The Item Created Event. This event exposes the new item being created with the ability to modify \ cancel the create request
        /// </summary>
        /// <remarks>
        ///     Created by NVH in version 1.6.1
        /// </remarks>
        public event NewItemCreatedEventHandler OnNewItemCreated;

        /// <summary>
        /// Item deleting event args
        /// </summary>
        public class ItemDeletingEventArgs : EventArgs
        {
            public ItemDeletingEventArgs()
            {
                this.Cancel = false;
            }

            public bool Cancel { get; set; }
            public object Item { get; set; }
        }

        /// <summary>
        /// New item created event args
        /// </summary>
        public class NewItemCreatedEventArgs : EventArgs
        {
            public NewItemCreatedEventArgs()
            {
                Cancel = false;
            }
            public object NewItem { get; set; }
            public bool Cancel { get; set; }
        }

        #endregion

Second is to implement the events, this is done in the following two methods. Here are the changes to the existing methods in collectioneditor.cs

private void Delete(object sender, ExecutedRoutedEventArgs e)
        {
            if (this.OnItemDeleting != null)
            {
                var eventArgs = new ItemDeletingEventArgs { Item = e.Parameter, Cancel = false };
                this.OnItemDeleting(this, eventArgs);
                if (eventArgs.Cancel)
                    return;
            }
            Items.Remove(e.Parameter);
            if (this.OnItemDeleted != null)
                this.OnItemDeleted(this, new EventArgs());
        }

private void AddNew(object sender, ExecutedRoutedEventArgs e)
        {
            var newItem = CreateNewItem((Type)e.Parameter);
            if (this.OnNewItemCreated != null)
            {
                var createEventArgs = new NewItemCreatedEventArgs();
                createEventArgs.NewItem = newItem;
                this.OnNewItemCreated(this, createEventArgs);
                if (createEventArgs.Cancel)
                    return;
            }
            Items.Add(newItem);
            SelectedItem = newItem;
        }

Hope to see more events throughout this toolkit!

Thanks for all your hard work...

 

Nico

Aug 20, 2012 at 6:20 PM

A feature request has been created based on this discussion:

http://wpftoolkit.codeplex.com/workitem/18505