Skip to content

Commit 4de9ca9

Browse files
ricardobossanRicardo Bossan (BEYONDSOFT CONSULTING INC)
andauthored
Ports DataMemberFieldEditor to the Runtime (#12891)
Ports DataMemberFieldEditor to the Runtime Related #1115 - Ports `DataMemberFieldEditor` to the Runtime. - Will add functionality to the `DatamemberFieldEditor` in the Runtime. - Minimal - Manual - `10.0.100-alpha.1.25077.2` Co-authored-by: Ricardo Bossan (BEYONDSOFT CONSULTING INC) <[email protected]>
1 parent a6268c4 commit 4de9ca9

File tree

6 files changed

+147
-3
lines changed

6 files changed

+147
-3
lines changed

src/System.Design/src/System.Design.Forwards.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.BindingSourceDesigner))]
99
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.ColumnHeaderCollectionEditor))]
1010
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.DataMemberFieldConverter))]
11+
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.DataMemberFieldEditor))]
1112
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.DataGridViewCellStyleEditor))]
1213
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.DataGridViewColumnTypeEditor))]
1314
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.DataSourceListEditor))]
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
#nullable enable
5+
6+
using System.ComponentModel;
7+
using System.Drawing.Design;
8+
9+
namespace System.Windows.Forms.Design;
10+
11+
internal class DataMemberFieldEditor : UITypeEditor
12+
{
13+
private DesignBindingPicker? _designBindingPicker;
14+
15+
public override bool IsDropDownResizable => true;
16+
17+
public override object? EditValue(ITypeDescriptorContext? context, IServiceProvider provider, object? value)
18+
{
19+
if (provider is null || context is null || context?.Instance is not { } instance)
20+
{
21+
return value;
22+
}
23+
24+
if (TypeDescriptor.GetProperties(instance)[nameof(ComboBox.DataSource)] is not PropertyDescriptor property)
25+
{
26+
return value;
27+
}
28+
29+
object? dataSource = property.GetValue(instance);
30+
31+
if (dataSource is null )
32+
{
33+
return value;
34+
}
35+
36+
_designBindingPicker ??= new();
37+
38+
DesignBinding oldSelection = new DesignBinding(dataSource, (string?)value);
39+
DesignBinding? newSelection = _designBindingPicker.Pick(
40+
context,
41+
provider,
42+
showDataSources: false,
43+
showDataMembers: true,
44+
selectListMembers: false,
45+
rootDataSource: dataSource,
46+
rootDataMember: string.Empty,
47+
initialSelectedItem: oldSelection
48+
);
49+
50+
if (newSelection is null)
51+
{
52+
return value;
53+
}
54+
55+
return newSelection.DataMember;
56+
}
57+
58+
public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext? context) => UITypeEditorEditStyle.DropDown;
59+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
4+
#nullable enable
5+
6+
using System.ComponentModel;
7+
using System.Drawing.Design;
8+
using Moq;
9+
10+
namespace System.Windows.Forms.Design.Editors.Tests;
11+
12+
public class DataMemberFieldEditorTests
13+
{
14+
private readonly DataMemberFieldEditor _editor = new();
15+
16+
[Fact]
17+
public void Ctor_HasDefaultProperties() => _editor.IsDropDownResizable.Should().BeTrue();
18+
19+
[Fact]
20+
public void GetEditStyle_ContextIsNull_ReturnsDropDown() => _editor.GetEditStyle(null).Should().Be(UITypeEditorEditStyle.DropDown);
21+
22+
public static IEnumerable<object[]> EditValueCases()
23+
{
24+
string text = "Edited Text";
25+
26+
Mock<ITypeDescriptorContext>? contextMock = new();
27+
contextMock.Setup(c => c.Instance).Returns(new ComboBox());
28+
29+
Mock<IServiceProvider> providerMock = new();
30+
providerMock.Setup(p => p.GetService(typeof(IWindowsFormsEditorService))).Returns(new Mock<IWindowsFormsEditorService>().Object);
31+
32+
yield return new object[] { null!, null!, null! };
33+
yield return new object[] { null!, null!, text };
34+
35+
yield return new object[]
36+
{
37+
contextMock.Object,
38+
providerMock.Object,
39+
text
40+
};
41+
}
42+
43+
[Theory]
44+
[MemberData(nameof(EditValueCases))]
45+
public void EditValue_WithValidInput_ReturnsValue(ITypeDescriptorContext? context, IServiceProvider provider, object? value)
46+
{
47+
object? result = _editor.EditValue(context, provider, value);
48+
49+
result.Should().Be(value);
50+
}
51+
}

src/System.Windows.Forms/tests/IntegrationTests/WinformsControlsTest/ListBoxes.Designer.cs

Lines changed: 30 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/System.Windows.Forms/tests/IntegrationTests/WinformsControlsTest/ListBoxes.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ public partial class ListBoxes : Form
1111
public ListBoxes()
1212
{
1313
InitializeComponent();
14+
MinimumSize = new Size(Width, 900);
1415
}
1516

1617
private void addButton_Click(object sender, EventArgs e)
@@ -60,4 +61,9 @@ private void ListBox_MeasureItem(object sender, MeasureItemEventArgs e)
6061
e.ItemHeight += 5;
6162
}
6263
}
64+
65+
private void Control_Click(object sender, EventArgs e)
66+
{
67+
propertyGrid.SelectedObject = sender;
68+
}
6369
}

src/System.Windows.Forms/tests/UnitTests/System/Windows/Forms/Design/DesignerAttributeTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ public class DesignerAttributeTests
2626
$"System.Windows.Forms.Design.DataGridDesigner, {Assemblies.SystemDesign}",
2727
$"System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, {Assemblies.SystemDesign}",
2828
$"System.Windows.Forms.Design.DataGridViewComponentEditor, {Assemblies.SystemDesign}",
29-
$"System.Windows.Forms.Design.DataMemberFieldEditor, {Assemblies.SystemDesign}",
3029
$"System.Windows.Forms.Design.StatusBarDesigner, {Assemblies.SystemDesign}",
3130
$"System.Windows.Forms.Design.ToolBarButtonDesigner, {Assemblies.SystemDesign}",
3231
$"System.Windows.Forms.Design.ToolBarDesigner, {Assemblies.SystemDesign}",

0 commit comments

Comments
 (0)