From c7616c265eb5210e4d1cb39ee16fa5139d097f2c Mon Sep 17 00:00:00 2001 From: Vladimir Krestov Date: Fri, 25 Oct 2019 12:33:49 +0300 Subject: [PATCH] Ported ListViewGroupCollectionEditor --- .../src/System.Design.Forwards.cs | 1 + .../Design/ListViewGroupCollectionEditor.cs | 79 +++++++++++++++++++ .../tests/UnitTests/EnsureEditorsTests.cs | 2 +- 3 files changed, 81 insertions(+), 1 deletion(-) create mode 100644 src/System.Windows.Forms.Design.Editors/src/System/Windows/Forms/Design/ListViewGroupCollectionEditor.cs diff --git a/src/System.Design/src/System.Design.Forwards.cs b/src/System.Design/src/System.Design.Forwards.cs index 197f9c5c4e4..ef75c715d94 100644 --- a/src/System.Design/src/System.Design.Forwards.cs +++ b/src/System.Design/src/System.Design.Forwards.cs @@ -9,6 +9,7 @@ [assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.ImageCollectionEditor))] [assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.ImageIndexEditor))] [assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.ListControlStringCollectionEditor))] +[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.ListViewGroupCollectionEditor))] [assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.ListViewSubItemCollectionEditor))] [assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.StringArrayEditor))] [assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.StringCollectionEditor))] diff --git a/src/System.Windows.Forms.Design.Editors/src/System/Windows/Forms/Design/ListViewGroupCollectionEditor.cs b/src/System.Windows.Forms.Design.Editors/src/System/Windows/Forms/Design/ListViewGroupCollectionEditor.cs new file mode 100644 index 00000000000..db082f459eb --- /dev/null +++ b/src/System.Windows.Forms.Design.Editors/src/System/Windows/Forms/Design/ListViewGroupCollectionEditor.cs @@ -0,0 +1,79 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.ComponentModel; +using System.ComponentModel.Design; +using System.ComponentModel.Design.Serialization; + +namespace System.Windows.Forms.Design +{ + /// + /// Provides an editor for a ListView groups collection. + /// + internal class ListViewGroupCollectionEditor : CollectionEditor + { + private object _editValue; + + public ListViewGroupCollectionEditor(Type type) : base(type) + { } + + /// + /// Creates a ListViewGroup instance. + /// + protected override object CreateInstance(Type itemType) + { + ListViewGroup lvg = (ListViewGroup)base.CreateInstance(itemType); + + // Create an unique name for the list view group. + lvg.Name = CreateListViewGroupName((ListViewGroupCollection)_editValue); + + return lvg; + } + + private string CreateListViewGroupName(ListViewGroupCollection lvgCollection) + { + string lvgName = "ListViewGroup"; + string resultName; + INameCreationService ncs = GetService(typeof(INameCreationService)) as INameCreationService; + IContainer container = GetService(typeof(IContainer)) as IContainer; + + if (ncs != null && container != null) + { + lvgName = ncs.CreateName(container, typeof(ListViewGroup)); + } + + // strip the digits from the end. + while (char.IsDigit(lvgName[lvgName.Length - 1])) + { + lvgName = lvgName.Substring(0, lvgName.Length - 1); + } + + int i = 1; + resultName = lvgName + i.ToString(System.Globalization.CultureInfo.CurrentCulture); + + while (lvgCollection[resultName] != null) + { + i++; + resultName = lvgName + i.ToString(System.Globalization.CultureInfo.CurrentCulture); + } + + return resultName; + } + + public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value) + { + _editValue = value; + object ret; + + // This will block while the ListViewGroupCollectionDialog is running. + ret = base.EditValue(context, provider, value); + + // The user is done w/ the ListViewGroupCollectionDialog. + // Don't need the edit value any longer + _editValue = null; + + return ret; + } + } +} diff --git a/src/System.Windows.Forms.Design.Editors/tests/UnitTests/EnsureEditorsTests.cs b/src/System.Windows.Forms.Design.Editors/tests/UnitTests/EnsureEditorsTests.cs index 480fb206669..9e8818827a1 100644 --- a/src/System.Windows.Forms.Design.Editors/tests/UnitTests/EnsureEditorsTests.cs +++ b/src/System.Windows.Forms.Design.Editors/tests/UnitTests/EnsureEditorsTests.cs @@ -89,7 +89,7 @@ public void EnsureUITypeEditorForType(Type type, Type expectedEditorType) //[InlineData(typeof(ListControl), "FormatString", typeof(FormatStringEditor))] //[InlineData(typeof(ListControl), "ValueMember", typeof(DataMemberFieldEditor))] //[InlineData(typeof(ListView), "Columns", typeof(ColumnHeaderCollectionEditor))] - //[InlineData(typeof(ListView), "Groups", typeof(ListViewGroupCollectionEditor))] + [InlineData(typeof(ListView), "Groups", typeof(ListViewGroupCollectionEditor))] //[InlineData(typeof(ListView), "Items", typeof(ListViewItemCollectionEditor))] [InlineData(typeof(ListViewItem), "ImageIndex", typeof(ImageIndexEditor))] [InlineData(typeof(ListViewItem), "ImageKey", typeof(ImageIndexEditor))]