Skip to content

Ports DataMemberFieldEditor to the Runtime #12891

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/System.Design/src/System.Design.Forwards.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.BindingSourceDesigner))]
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.ColumnHeaderCollectionEditor))]
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.DataMemberFieldConverter))]
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.DataMemberFieldEditor))]
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.DataGridViewCellStyleEditor))]
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.DataGridViewColumnTypeEditor))]
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.DataSourceListEditor))]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable enable

using System.ComponentModel;
using System.Drawing.Design;

namespace System.Windows.Forms.Design;

internal class DataMemberFieldEditor : UITypeEditor
{
private DesignBindingPicker? _designBindingPicker;

public override bool IsDropDownResizable => true;

public override object? EditValue(ITypeDescriptorContext? context, IServiceProvider provider, object? value)
{
if (provider is null || context is null || context?.Instance is not { } instance)
{
return value;
}

if (TypeDescriptor.GetProperties(instance)[nameof(ComboBox.DataSource)] is not PropertyDescriptor property)
{
return value;
}

object? dataSource = property.GetValue(instance);

if (dataSource is null )
{
return value;
}

_designBindingPicker ??= new();

DesignBinding oldSelection = new DesignBinding(dataSource, (string?)value);
DesignBinding? newSelection = _designBindingPicker.Pick(
context,
provider,
showDataSources: false,
showDataMembers: true,
selectListMembers: false,
rootDataSource: dataSource,
rootDataMember: string.Empty,
initialSelectedItem: oldSelection
);

if (newSelection is null)
{
return value;
}

return newSelection.DataMember;
}

public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext? context) => UITypeEditorEditStyle.DropDown;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable enable

using System.ComponentModel;
using System.Drawing.Design;
using Moq;

namespace System.Windows.Forms.Design.Editors.Tests;

public class DataMemberFieldEditorTests
{
private readonly DataMemberFieldEditor _editor = new();

[Fact]
public void Ctor_HasDefaultProperties() => _editor.IsDropDownResizable.Should().BeTrue();

[Fact]
public void GetEditStyle_ContextIsNull_ReturnsDropDown() => _editor.GetEditStyle(null).Should().Be(UITypeEditorEditStyle.DropDown);

public static IEnumerable<object[]> EditValueCases()
{
string text = "Edited Text";

Mock<ITypeDescriptorContext>? contextMock = new();
contextMock.Setup(c => c.Instance).Returns(new ComboBox());

Mock<IServiceProvider> providerMock = new();
providerMock.Setup(p => p.GetService(typeof(IWindowsFormsEditorService))).Returns(new Mock<IWindowsFormsEditorService>().Object);

yield return new object[] { null!, null!, null! };
yield return new object[] { null!, null!, text };

yield return new object[]
{
contextMock.Object,
providerMock.Object,
text
};
}

[Theory]
[MemberData(nameof(EditValueCases))]
public void EditValue_WithValidInput_ReturnsValue(ITypeDescriptorContext? context, IServiceProvider provider, object? value)
{
object? result = _editor.EditValue(context, provider, value);

result.Should().Be(value);
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public partial class ListBoxes : Form
public ListBoxes()
{
InitializeComponent();
MinimumSize = new Size(Width, 900);
}

private void addButton_Click(object sender, EventArgs e)
Expand Down Expand Up @@ -60,4 +61,9 @@ private void ListBox_MeasureItem(object sender, MeasureItemEventArgs e)
e.ItemHeight += 5;
}
}

private void Control_Click(object sender, EventArgs e)
{
propertyGrid.SelectedObject = sender;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public class DesignerAttributeTests
$"System.Windows.Forms.Design.DataGridDesigner, {Assemblies.SystemDesign}",
$"System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, {Assemblies.SystemDesign}",
$"System.Windows.Forms.Design.DataGridViewComponentEditor, {Assemblies.SystemDesign}",
$"System.Windows.Forms.Design.DataMemberFieldEditor, {Assemblies.SystemDesign}",
$"System.Windows.Forms.Design.StatusBarDesigner, {Assemblies.SystemDesign}",
$"System.Windows.Forms.Design.ToolBarButtonDesigner, {Assemblies.SystemDesign}",
$"System.Windows.Forms.Design.ToolBarDesigner, {Assemblies.SystemDesign}",
Expand Down