From 58589e966bfa29e8199395a5a3517e54a3f7811b Mon Sep 17 00:00:00 2001 From: "Ricardo Bossan (BEYONDSOFT CONSULTING INC) (from Dev Box)" Date: Thu, 11 Sep 2025 02:24:44 -0300 Subject: [PATCH 1/2] Makes PropertyGrid for CoboBox show "(none)" when DataSource is null for ComboBox --- .../Forms/Design/DataSourceConverter.cs | 24 ++++++++++++++ .../Forms/Design/DataSourceConverterTests.cs | 32 +++++++++++++++++++ .../Forms/Controls/ComboBox/ComboBox.cs | 1 + 3 files changed, 57 insertions(+) create mode 100644 src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DataSourceConverter.cs create mode 100644 src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/DataSourceConverterTests.cs diff --git a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DataSourceConverter.cs b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DataSourceConverter.cs new file mode 100644 index 00000000000..092e6b6cdae --- /dev/null +++ b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DataSourceConverter.cs @@ -0,0 +1,24 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.ComponentModel; +using System.Globalization; + +namespace System.Windows.Forms.Design; + +internal class DataSourceConverter : ReferenceConverter +{ + public DataSourceConverter() : base(typeof(IListSource)) + { + } + + public override object? ConvertTo(ITypeDescriptorContext? context, CultureInfo? culture, object? value, Type destinationType) + { + if (destinationType == typeof(string) && value is null) + { + return SR.None_lc; + } + + return base.ConvertTo(context, culture, value, destinationType); + } +} diff --git a/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/DataSourceConverterTests.cs b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/DataSourceConverterTests.cs new file mode 100644 index 00000000000..9b6f68f1c09 --- /dev/null +++ b/src/System.Windows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/DataSourceConverterTests.cs @@ -0,0 +1,32 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +namespace System.Windows.Forms.Design.Tests; + +public class DataSourceConverterTests +{ + private readonly DataSourceConverter _converter = new(); + + [Fact] + public void ConvertTo_NullValueToString_ReturnsNoneLowercase() => + (_converter.ConvertTo(context: null, culture: null, value: null, destinationType: typeof(string)) as string).Should().Be("(none)"); + + [Fact] + public void ConvertTo_NonNullValueToString_CallsBase() => + _converter.ConvertTo(context: null, culture: null, value: new(), destinationType: typeof(string)).Should().NotBe("(none)"); + + [Fact] + public void ConvertTo_NullValueToNonString_ThrowsNotSupportedException() + { + Action act = () => _converter.ConvertTo(context: null, culture: null, value: null, destinationType: typeof(int)); + + act.Should().Throw(); + } + + [Theory] + [InlineData("en-US")] + [InlineData("fr-FR")] + [InlineData("de-DE")] + public void ConvertTo_NullValueWithDifferentCultures_ReturnsNoneLowercase(string cultureName) => + (_converter.ConvertTo(context: null, culture: new(cultureName), value: null, destinationType: typeof(string)) as string).Should().Be("(none)"); +} diff --git a/src/System.Windows.Forms/System/Windows/Forms/Controls/ComboBox/ComboBox.cs b/src/System.Windows.Forms/System/Windows/Forms/Controls/ComboBox/ComboBox.cs index cf37b868689..2491fb6af40 100644 --- a/src/System.Windows.Forms/System/Windows/Forms/Controls/ComboBox/ComboBox.cs +++ b/src/System.Windows.Forms/System/Windows/Forms/Controls/ComboBox/ComboBox.cs @@ -405,6 +405,7 @@ protected override Size DefaultSize [RefreshProperties(RefreshProperties.Repaint), AttributeProvider(typeof(IListSource))] [SRDescription(nameof(SR.ListControlDataSourceDescr))] + [TypeConverter("System.Windows.Forms.Design.DataSourceConverter, System.Windows.Forms.Design")] public new object? DataSource { get => base.DataSource; From 9120a2bd88dc3b85140299abc90ac66b04f923e2 Mon Sep 17 00:00:00 2001 From: "Ricardo Bossan (BEYONDSOFT CONSULTING INC) (from Dev Box)" Date: Mon, 15 Sep 2025 13:01:31 -0300 Subject: [PATCH 2/2] Converts DataSourceConverter value to string, for parity with NetFx --- .../src/System/Windows/Forms/Design/DataSourceConverter.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DataSourceConverter.cs b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DataSourceConverter.cs index 092e6b6cdae..7698bec6c70 100644 --- a/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DataSourceConverter.cs +++ b/src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DataSourceConverter.cs @@ -16,7 +16,7 @@ public DataSourceConverter() : base(typeof(IListSource)) { if (destinationType == typeof(string) && value is null) { - return SR.None_lc; + return SR.None_lc.ToString(); } return base.ConvertTo(context, culture, value, destinationType);