| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.ComponentModel.Design; |
| | 4 | |
|
| | 5 | | namespace PropertyGridHelpers.UIEditors |
| | 6 | | { |
| | 7 | | /// <summary> |
| | 8 | | /// Provides a strongly-typed collection editor for editing <see cref="List{T}"/> properties |
| | 9 | | /// in a <see cref="System.Windows.Forms.PropertyGrid"/>. |
| | 10 | | /// This allows users to add, remove, and edit items of type <typeparamref name="T"/> |
| | 11 | | /// using the standard collection editor dialog. |
| | 12 | | /// </summary> |
| | 13 | | /// <typeparam name="T">The type of the items in the collection being edited.</typeparam> |
| | 14 | | /// <seealso cref="CollectionEditor"/> |
| | 15 | | /// <remarks> |
| | 16 | | /// This editor automatically uses a <see cref="List{T}"/> as the collection type, |
| | 17 | | /// and supports simple creation of string elements or any other type with a |
| | 18 | | /// parameterless constructor. |
| | 19 | | /// </remarks> |
| | 20 | | public class CollectionUIEditor<T> : CollectionEditor |
| | 21 | | { |
| | 22 | | /// <summary> |
| | 23 | | /// Initializes a new instance of the <see cref="CollectionUIEditor{T}"/> class, |
| | 24 | | /// targeting collections of type <see cref="List{T}"/>. |
| | 25 | | /// </summary> |
| 16 | 26 | | public CollectionUIEditor() : base(type: typeof(List<T>)) |
| 8 | 27 | | { |
| | 28 | |
|
| 16 | 29 | | } |
| | 30 | |
|
| | 31 | | /// <summary> |
| | 32 | | /// Creates a new instance of the specified <paramref name="itemType"/> to add to the collection. |
| | 33 | | /// </summary> |
| | 34 | | /// <param name="itemType">The type of the item to create.</param> |
| | 35 | | /// <returns> |
| | 36 | | /// A new instance of <paramref name="itemType"/>. For strings, an empty string is returned. |
| | 37 | | /// </returns> |
| | 38 | | /// <remarks> |
| | 39 | | /// This override ensures that <c>string</c> elements are initialized to an empty value, |
| | 40 | | /// while other types use their parameterless constructor. |
| | 41 | | /// </remarks> |
| | 42 | | protected override object CreateInstance(Type itemType) => |
| 16 | 43 | | itemType == typeof(string) ? string.Empty : (object)Activator.CreateInstance(itemType); |
| | 44 | | } |
| | 45 | | } |