Skip to content

Commit

Permalink
Port DataGridViewColumnDataPropertyNameEditor and DataMemberListEditor (
Browse files Browse the repository at this point in the history
dotnet#12902)

* port DataGridViewColumnDataPropertyNameEditor   and DataMemberListEditor
  • Loading branch information
Epica3055 authored Feb 14, 2025
1 parent e2e2a39 commit dd64431
Show file tree
Hide file tree
Showing 6 changed files with 198 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/System.Design/src/System.Design.Forwards.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.StringCollectionEditor))]
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.TabPageCollectionEditor))]
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.TreeNodeCollectionEditor))]
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.DataMemberListEditor))]
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.DataGridViewColumnDataPropertyNameEditor))]

// internal serializers
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.ControlCodeDomSerializer))]
Expand Down
Original file line number Diff line number Diff line change
@@ -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.

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

namespace System.Windows.Forms.Design;
internal class DataGridViewColumnDataPropertyNameEditor : UITypeEditor
{
public DataGridViewColumnDataPropertyNameEditor() : base() { }

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 null)
{
return value;
}

DataGridView? dataGridView;
if (context.Instance is DataGridViewColumnCollectionDialog.ListBoxItem item)
{
dataGridView = item.DataGridViewColumn.DataGridView;
}
else
{
dataGridView = (context.Instance as DataGridViewColumn)?.DataGridView;
}

if (dataGridView is null)
{
return value;
}

object? dataSource = dataGridView.DataSource;
string? dataMember = dataGridView.DataMember;

ArgumentNullException.ThrowIfNull(value);
string valueString = (string)value;
string? selectedMember = $"{dataMember}.{valueString}";
if (dataSource is null)
{
dataMember = string.Empty;
selectedMember = valueString;
}

_designBindingPicker ??= new();

DesignBinding oldSelection = new(dataSource, selectedMember);
DesignBinding? newSelection = _designBindingPicker.Pick(context,
provider,
showDataSources: false,
showDataMembers: true,
selectListMembers: false,
dataSource,
dataMember,
oldSelection);
if (dataSource is not null && newSelection is not null)
{
value = newSelection.DataField;
}

return value;
}

public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext? context) => UITypeEditorEditStyle.DropDown;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// 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.Drawing.Design;

namespace System.Windows.Forms.Design;

internal class DataMemberListEditor : 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 null)
{
return value;
}

if (TypeDescriptor.GetProperties(context.Instance)[nameof(ComboBox.DataSource)] is { } dataSourceProperty)
{
object? dataSource = dataSourceProperty.GetValue(context.Instance);
_designBindingPicker ??= new();

DesignBinding oldSelection = new(dataSource, value as string);
DesignBinding? newSelection = _designBindingPicker.Pick(context,
provider,
showDataSources: false,
showDataMembers: true,
selectListMembers: true,
dataSource,
string.Empty,
oldSelection);
if (dataSource is not null && newSelection is not null)
{
value = newSelection.DataMember;
}
}

return value;
}

public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext? context) => UITypeEditorEditStyle.DropDown;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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.Drawing.Design;
using Moq;

namespace System.Windows.Forms.Design.Tests;

public class DataGridViewColumnDataPropertyNameEditorTests
{
[Fact]
public void DataGridViewColumnDataPropertyNameEditor_GetEditStyle()
{
new DataGridViewColumnDataPropertyNameEditor().GetEditStyle().Should().Be(UITypeEditorEditStyle.DropDown);
}

[Fact]
public void DataGridViewColumnDataPropertyNameEditor_IsDropDownResizable()
{
new DataGridViewColumnDataPropertyNameEditor().IsDropDownResizable.Should().Be(true);
}

[Fact]
public void DataGridViewColumnDataPropertyNameEditor_EditValue()
{
DataGridViewColumnDataPropertyNameEditor dataGridViewColumnEditor = new();
object value = "123";
dataGridViewColumnEditor.EditValue(null, null, value).Should().Be(value);

Mock<ITypeDescriptorContext> mockTypeDescriptorContext = new(MockBehavior.Strict);
dataGridViewColumnEditor.EditValue(mockTypeDescriptorContext.Object, null, value).Should().Be(value);

Mock<IServiceProvider> mockServiceProvider = new(MockBehavior.Strict);
dataGridViewColumnEditor.EditValue(null, mockServiceProvider.Object, value).Should().Be(value);

mockTypeDescriptorContext.Setup(x => x.Instance).Returns(null);
dataGridViewColumnEditor.EditValue(null, mockServiceProvider.Object, value).Should().Be(value);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// 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.Drawing.Design;
using Moq;

namespace System.Windows.Forms.Design.Tests;

public class DataMemberListEditorTests
{
[Fact]
public void DataMemberListEditor_GetEditStyle()
{
new DataMemberListEditor().GetEditStyle().Should().Be(UITypeEditorEditStyle.DropDown);
}

[Fact]
public void DataMemberListEditor_IsDropDownResizable()
{
new DataMemberListEditor().IsDropDownResizable.Should().Be(true);
}

[Fact]
public void DataMemberListEditor_EditValue()
{
DataMemberListEditor dataMemberListEditor = new();
object value = "123";
dataMemberListEditor.EditValue(null, null, value).Should().Be(value);

Mock<ITypeDescriptorContext> mockTypeDescriptorContext = new(MockBehavior.Strict);
dataMemberListEditor.EditValue(mockTypeDescriptorContext.Object, null, value).Should().Be(value);

Mock<IServiceProvider> mockServiceProvider = new(MockBehavior.Strict);
dataMemberListEditor.EditValue(null, mockServiceProvider.Object, value).Should().Be(value);

mockTypeDescriptorContext.Setup(x => x.Instance).Returns(null);
dataMemberListEditor.EditValue(null, mockServiceProvider.Object, value).Should().Be(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,8 @@ public class DesignerAttributeTests
$"System.Windows.Forms.Design.DataGridTableStyleMappingNameEditor, {Assemblies.SystemDesign}",
$"System.Windows.Forms.Design.DataGridDesigner, {Assemblies.SystemDesign}",
$"System.Windows.Forms.Design.DataGridViewColumnCollectionEditor, {Assemblies.SystemDesign}",
$"System.Windows.Forms.Design.DataGridViewColumnDataPropertyNameEditor, {Assemblies.SystemDesign}",
$"System.Windows.Forms.Design.DataGridViewComponentEditor, {Assemblies.SystemDesign}",
$"System.Windows.Forms.Design.DataMemberFieldEditor, {Assemblies.SystemDesign}",
$"System.Windows.Forms.Design.DataMemberListEditor, {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

0 comments on commit dd64431

Please sign in to comment.