Skip to content
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

Port DataGridViewComboBoxColumnDesigner to runtime #9869

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 @@ -39,6 +39,7 @@
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.ComboBoxDesigner))]
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.DataGridViewDesigner))]
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.DataGridViewColumnDesigner))]
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.DataGridViewComboBoxColumnDesigner))]
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.DateTimePickerDesigner))]
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.FormDocumentDesigner))]
[assembly: TypeForwardedTo(typeof(System.Windows.Forms.Design.FlowPanelDesigner))]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
// 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.Collections;

namespace System.Windows.Forms.Design;

/// <summary>
/// Provides a base designer for data grid view columns.
/// </summary>
internal class DataGridViewComboBoxColumnDesigner : DataGridViewColumnDesigner
{
private static BindingContext? s_bindingContext;

private string ValueMember
{
get
{
DataGridViewComboBoxColumn dataGridViewComboBoxColumn = (DataGridViewComboBoxColumn)Component;
return dataGridViewComboBoxColumn.ValueMember;
}
set
{
DataGridViewComboBoxColumn dataGridViewComboBoxColumn = (DataGridViewComboBoxColumn)Component;
if (dataGridViewComboBoxColumn.DataSource is null)
{
return;
}

if (ValidDataMember(dataGridViewComboBoxColumn.DataSource, dataMember: value))
{
dataGridViewComboBoxColumn.ValueMember = value;
}
}
}

private string DisplayMember
{
get
{
DataGridViewComboBoxColumn dataGridViewComboBoxColumn = (DataGridViewComboBoxColumn)Component;
return dataGridViewComboBoxColumn.DisplayMember;
}
set
{
DataGridViewComboBoxColumn dataGridViewComboBoxColumn = (DataGridViewComboBoxColumn)Component;
if (dataGridViewComboBoxColumn.DataSource is null)
{
return;
}

if (ValidDataMember(dataGridViewComboBoxColumn.DataSource, value))
{
dataGridViewComboBoxColumn.DisplayMember = value;
}
}
}

private bool ShouldSerializeDisplayMember()
{
DataGridViewComboBoxColumn dataGridViewComboBoxColumn = (DataGridViewComboBoxColumn)Component;
return !string.IsNullOrEmpty(dataGridViewComboBoxColumn.DisplayMember);
}

private bool ShouldSerializeValueMember()
{
DataGridViewComboBoxColumn dataGridViewComboBoxColumn = (DataGridViewComboBoxColumn)Component;
return !string.IsNullOrEmpty(dataGridViewComboBoxColumn.ValueMember);
}

private static bool ValidDataMember(object dataSource, string dataMember)
{
if (string.IsNullOrEmpty(dataMember))
{
// A null string is a valid value
return true;
}

s_bindingContext ??= new BindingContext();

BindingMemberInfo bindingMemberInfo = new BindingMemberInfo(dataMember);
BindingManagerBase bindingManagerBase;

try
{
bindingManagerBase = s_bindingContext[dataSource, bindingMemberInfo.BindingPath];
}
catch (ArgumentException)
{
return false;
}

return bindingManagerBase is null
? false
: (bindingManagerBase.GetItemProperties()?[bindingMemberInfo.BindingField]) is not null;
}

protected override void PreFilterProperties(IDictionary properties)
{
base.PreFilterProperties(properties);

PropertyDescriptor? property = (PropertyDescriptor?)properties["ValueMember"];
if (property is not null)
{
properties["ValueMember"] = TypeDescriptor.CreateProperty(typeof(DataGridViewComboBoxColumnDesigner), property, Array.Empty<Attribute>());
}

property = (PropertyDescriptor?)properties["DisplayMember"];
if (property is not null)
{
properties["DisplayMember"] = TypeDescriptor.CreateProperty(typeof(DataGridViewComboBoxColumnDesigner), property, Array.Empty<Attribute>());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,10 @@ private void CreateDesignSurface(int n)
BindingSource bindingSource = surface.CreateComponent<BindingSource>();
bindingSource.DataSource = new List<string> { "a1", "b2", "c3", "d4", "e5", "f6" };
listBox.DataSource = bindingSource;
surface.CreateControl<DataGridView>(new Size(200, 150), new Point(470, 220));
DataGridView dataGridView = surface.CreateControl<DataGridView>(new Size(200, 150), new Point(470, 220));
DataGridViewComboBoxColumn comboBoxColumn = surface.CreateComponent<DataGridViewComboBoxColumn>();
comboBoxColumn.HeaderText = "Column1";
dataGridView.Columns.AddRange([comboBoxColumn]);
}

break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ public class DesignerAttributeTests
// https://github.com/dotnet/winforms/issues/2411
"System.Windows.Forms.Design.AxDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Windows.Forms.Design.AxHostDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Windows.Forms.Design.DataGridViewComboBoxColumnDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Windows.Forms.Design.StatusBarDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",
"System.Windows.Forms.Design.WebBrowserDesigner, System.Design, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a",

Expand Down
Loading