diff --git a/src/System.Design/src/System.Design.Forwards.cs b/src/System.Design/src/System.Design.Forwards.cs index 22e156a61d3..521fa88fbce 100644 --- a/src/System.Design/src/System.Design.Forwards.cs +++ b/src/System.Design/src/System.Design.Forwards.cs @@ -3,7 +3,7 @@ // See the LICENSE file in the project root for more information. using System.Runtime.CompilerServices; - +[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.ColumnHeaderCollectionEditor))] [assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.DataMemberFieldConverter))] [assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.FormatStringEditor))] [assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.HelpNamespaceEditor))] diff --git a/src/System.Windows.Forms.Design.Editors/src/System/Windows/Forms/Design/ColumnHeaderCollectionEditor.cs b/src/System.Windows.Forms.Design.Editors/src/System/Windows/Forms/Design/ColumnHeaderCollectionEditor.cs new file mode 100644 index 00000000000..5191637013e --- /dev/null +++ b/src/System.Windows.Forms.Design.Editors/src/System/Windows/Forms/Design/ColumnHeaderCollectionEditor.cs @@ -0,0 +1,70 @@ +// 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; + +namespace System.Windows.Forms.Design +{ + internal class ColumnHeaderCollectionEditor : CollectionEditor + { + /// + /// Initializes a new instance of the class. + /// + public ColumnHeaderCollectionEditor(Type type) : base(type) + { + } + + /// + /// Gets the help topic to display for the dialog help button or pressing F1. Override to display a different help topic. + /// + protected override string HelpTopic + { + get => "net.ComponentModel.ColumnHeaderCollectionEditor"; + } + + /// + /// Sets the specified collection to have the specified array of items. + /// + protected override object SetItems(object editValue, object[] value) + { + if (editValue is ListView.ColumnHeaderCollection list) + { + list.Clear(); + ColumnHeader[] colHeaders = new ColumnHeader[value.Length]; + Array.Copy(value, 0, colHeaders, 0, value.Length); + list.AddRange(colHeaders); + } + return editValue; + } + + /// + /// Removes the item from listview column header collection + /// + internal override void OnItemRemoving(object item) + { + if (!(Context.Instance is ListView listview)) + { + return; + } + + if (item is ColumnHeader column) + { + IComponentChangeService cs = GetService(typeof(IComponentChangeService)) as IComponentChangeService; + PropertyDescriptor itemsProp = null; + if (cs != null) + { + itemsProp = TypeDescriptor.GetProperties(Context.Instance)["Columns"]; + cs.OnComponentChanging(Context.Instance, itemsProp); + } + listview.Columns.Remove(column); + + if (cs != null && itemsProp != null) + { + cs.OnComponentChanged(Context.Instance, itemsProp, null, null); + } + } + } + } +} diff --git a/src/System.Windows.Forms.Design.Editors/tests/UnitTests/System/Windows/Forms/Design/ColumnHeaderCollectionEditorTests .cs b/src/System.Windows.Forms.Design.Editors/tests/UnitTests/System/Windows/Forms/Design/ColumnHeaderCollectionEditorTests .cs new file mode 100644 index 00000000000..aa7e3eebd37 --- /dev/null +++ b/src/System.Windows.Forms.Design.Editors/tests/UnitTests/System/Windows/Forms/Design/ColumnHeaderCollectionEditorTests .cs @@ -0,0 +1,27 @@ +// 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 Xunit; + +namespace System.Windows.Forms.Design.Tests +{ + public class ColumnHeaderCollectionEditorTests + { + [Fact] + public void ColumnHeaderCollectionEditor_Ctor_Default() + { + var editor = new ColumnHeaderCollectionEditor(typeof(string)); + Assert.False(editor.IsDropDownResizable); + } + + [Fact] + public void ColumnHeaderCollectionEditor_EditValue_ReturnsValue() + { + var editor = new ColumnHeaderCollectionEditor(typeof(string)); + string[] value = new string[] { "asdf", "qwer", "zxcv" }; + + Assert.Same(value, editor.EditValue(null, value)); + } + } +}