forked from dotnet/winforms
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Port DataGridViewColumnDataPropertyNameEditor and DataMemberListEditor (
dotnet#12902) * port DataGridViewColumnDataPropertyNameEditor and DataMemberListEditor
- Loading branch information
Showing
6 changed files
with
198 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
....Forms.Design/src/System/Windows/Forms/Design/DataGridViewColumnDataPropertyNameEditor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
46 changes: 46 additions & 0 deletions
46
src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DataMemberListEditor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
40 changes: 40 additions & 0 deletions
40
...ts/UnitTests/System/Windows/Forms/Design/DataGridViewColumnDataPropertyNameEditorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
...ows.Forms.Design/tests/UnitTests/System/Windows/Forms/Design/DataMemberListEditorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters