将以下属性之一应用于您的自定义控件,具体取决于您需要哪种数据绑定:
(问题特别提到了 complex 数据绑定,但给定的代码示例对我来说看起来像 lookup 数据绑定,所以我将两者都包括在内。)
例如实现,看.NET Framework source code:
但这些实现对我来说看起来非常复杂,因此在您自己的自定义控件中嵌入现有控件(例如 DataGridView、ListBox 或 ComboBox)以利用其现有数据可能更容易绑定实现,而不是自己编写。 (如有必要,您可以使嵌入式控件不可见。)这是 Microsoft 在以下指南中展示的方法:
在这些指南中,他们创建了一个数据源以将自定义控件绑定到外部数据库,但看起来您只是在尝试将自定义控件绑定到内部集合,例如 List<T>。在这种情况下,下面的改编代码可能对您有用。
在 Visual Studio 的 Windows 窗体项目中,添加新的 UserControl。
对于复杂 数据绑定,将ComplexBindingPropertiesAttribute 应用于自定义控件。向它添加一个DataGridView 控件。添加DataSource 和DataMember 属性,并将它们挂钩到DataGridView 自己的属性中。
// ComplexBindingControl.cs
// Adapted from https://docs.microsoft.com/visualstudio/data-tools/create-a-windows-forms-user-control-that-supports-complex-data-binding
using System.ComponentModel;
using System.Windows.Forms;
namespace BindingDemo
{
[ComplexBindingProperties("DataSource", "DataMember")]
public partial class ComplexBindingControl : UserControl
{
public ComplexBindingControl()
{
InitializeComponent();
}
// Use a DataGridView for its complex data binding implementation.
public object DataSource
{
get => dataGridView1.DataSource;
set => dataGridView1.DataSource = value;
}
public string DataMember
{
get => dataGridView1.DataMember;
set => dataGridView1.DataMember = value;
}
}
}
对于lookup 数据绑定,将LookupBindingPropertiesAttribute 应用于自定义控件。向其添加ListBox 或ComboBox 控件。添加DataSource、DisplayMember、ValueMember 和LookupMember 属性,并将它们挂钩到ListBox 或ComboBox 自己的属性中。
// LookupBindingControl.cs
// Adapted from https://docs.microsoft.com/visualstudio/data-tools/create-a-windows-forms-user-control-that-supports-lookup-data-binding
using System.ComponentModel;
using System.Windows.Forms;
namespace BindingDemo
{
[LookupBindingProperties("DataSource", "DisplayMember", "ValueMember", "LookupMember")]
public partial class LookupBindingControl : UserControl
{
public LookupBindingControl()
{
InitializeComponent();
}
// Use a ListBox or ComboBox for its lookup data binding implementation.
public object DataSource
{
get => listBox1.DataSource;
set => listBox1.DataSource = value;
}
public string DisplayMember
{
get => listBox1.DisplayMember;
set => listBox1.DisplayMember = value;
}
public string ValueMember
{
get => listBox1.ValueMember;
set => listBox1.ValueMember = value;
}
public string LookupMember
{
get => listBox1.SelectedValue?.ToString();
set => listBox1.SelectedValue = value;
}
}
}
(编辑:感谢Frank's answer提醒我listBox1.SelectedValue可能是null。)
要对其进行测试,请在 Visual Studio 中构建项目,然后将自定义控件的实例添加到 Form。创建一些示例数据,并使用其相关属性将其绑定到自定义控件。
// Form1.cs
using System.Collections.Generic;
using System.Windows.Forms;
namespace BindingDemo
{
public partial class Form1 : Form
{
private readonly List<SomeObject> data;
public Form1()
{
InitializeComponent();
// Prepare some sample data.
data = new List<SomeObject>
{
new SomeObject("Alice"),
new SomeObject("Bob"),
new SomeObject("Carol"),
};
// Bind the data to your custom control...
// ...for "complex" data binding:
complexBindingControl1.DataSource = data;
// ...for "lookup" data binding:
lookupBindingControl1.DataSource = data;
lookupBindingControl1.DisplayMember = "Name";
lookupBindingControl1.ValueMember = "Name";
}
}
internal class SomeObject
{
public SomeObject(string name)
{
Name = name;
}
public string Name { get; set; }
}
}